How to import local packages in go?

Well, I figured out the problem. Basically Go starting path for import is $HOME/go/src So I just needed to add myapp in front of the package names, that is, the import should be: import ( “log” “net/http” “myapp/common” “myapp/routers” )

Type func with interface parameter incompatible error

The concept you’re looking for here is variance in the type system. Some type systems and types support covariance and contravariance, but Go’s interfaces do not. While an int can be passed to a function that expects interface{}, the same cannot be said about func(int) and func(interface{}), because interfaces do not behave covariantly. If type … Read more

Does Go have standard Err variables?

There are a few common idiomatic ways for a package author to make error returns. Fixed error variables, usually named Err… var ( ErrSomethingBad = errors.New(“some string”) ErrKindFoo = errors.New(“foo happened”) ) Error types, usually named …Error type SomeError struct { // extra information, whatever might be useful to callers // (or for making a … Read more

Why would I make() or new()?

Go has multiple ways of memory allocation and value initialization: &T{…}, &someLocalVar, new, make Allocation can also happen when creating composite literals. new can be used to allocate values such as integers, &int is illegal: new(Point) &Point{} // OK &Point{2, 3} // Combines allocation and initialization new(int) &int // Illegal // Works, but it is … Read more

How to collect values from N goroutines executed in a specific order?

Goroutines run concurrently, independently, so without explicit synchronization you can’t predict execution and completion order. So as it is, you can’t pair returned numbers with the input numbers. You can either return more data (e.g. the input number and the output, wrapped in a struct for example), or pass pointers to the worker functions (launched … Read more