-1

I am new to Go and functional paradigm. while working with database connections in golang I have to import mysql driver. And I came across "_" which is to mention blank identifier for variables and import packages which are soley for their side effects.

Searched for side-effects and found this side effects in es6

What I didn't understand is side effect of a function is dependent on a global variable which deviates the referential transparency of a pure function. But how does a package can bring side effect? Is it because the dependency on the package as side effect?

eg:

import _ "github.com/go-sql-driver/mysql"

db, err := sql.Open("mysql", "user:password@/dbname")

Here the import has "_" because Open statement is dependent on driver name?

venkatReddi
  • 133
  • 5
  • 18
  • 4
    This does not relate to the side-effects that functional programmers mean when they talk about pure functions. The side effect here is the the init function of the package is executed, which registers the driver. – Peter Jan 24 '18 at 17:19

1 Answers1

2

The main side-effect one is interested in with an _ import is the execution of init functions. A package can include an init function which will be executed when the program starts, before main is executed. In the case of DB drivers, this is used to register the driver so you can use it with sql.Open; expvar and pprof packages both register HTTP handlers in their init functions, and are similarly often used with _ imports.

This is documented in the Go spec section on package initialization.

Adrian
  • 42,911
  • 6
  • 107
  • 99
  • import does execuate `init` function and other function execution on global variable declaration, such as `var x = F()`. – leaf bebop Jan 24 '18 at 17:15
  • True, but a well-written package will limit its initialization side-effects to the `init` function and stick global variable declarations which do not produce side effects. But yes, technically it can cause side effects this way. – Adrian Jan 24 '18 at 17:16
  • Yes, but go does not provide a way to deal with order of multiple `init` function, so sometimes to make things happens first, global declaration to cause "side-effects" is a work-arond. And I do agree that it shall be avoided by any good practice. – leaf bebop Jan 24 '18 at 17:20
  • It doesn't provide any more control over the order of global variable execution than `init` execution; per the spec, `init`s are executed "in the order they appear in the source", while "package-level variables are initialized in declaration order but after any of the variables they depend on" - roughly equivalent, though the globals are a little more complicated. – Adrian Jan 24 '18 at 17:23
  • I mean, global variables are always before init execuation, which one can take advantage of. Both global variable and `init` order is quite fragile but the former is always before the latter. – leaf bebop Jan 24 '18 at 17:26
  • True, but I think we're getting beyond the question here - the main side effect one is generally trying to get from an `_` import is the execution of `init` functions. – Adrian Jan 24 '18 at 17:36