Setting timezone globally in golang

You can achieve what you want from inside your app using os.Setenv(“TZ”, “Africa/Cairo”), what matters is that you must call this before any other package uses anything from the time package. How to ensure that? Create a package that does nothing else except sets the timezone (later you may add other things to it, but … Read more

How to avoid indirect dependencies in go.mod file

Unfortunately, you can’t avoid them. Indirect dependency, is basically dependency that wasn’t listed in go.mod of your direct dependency, but is still required by it. In your case it happens, because you use github.com/gocolly/colly v1.2.0 as dependency and v1.2.0 of this package isn’t a module, because it doesn’t contain go.mod, so all it’s dependencies are … Read more

How to obtain all request headers in Go

Use Request.Header to access all headers. Because Header is a map[string][]string, two loops are required to access all headers. // Loop over header names for name, values := range r.Header { // Loop over all values for the name. for _, value := range values { fmt.Println(name, value) } }

Golang dynamically creating member of struct

You will need to use a map (of type map[string]interface{}) to work with dynamic JSON. Here is an example of creating a new map: // Initial declaration m := map[string]interface{}{ “key”: “value”, } // Dynamically add a sub-map m[“sub”] = map[string]interface{}{ “deepKey”: “deepValue”, } Unmarshalling JSON into a map looks like: var f interface{} err … Read more

Go interactive shell [duplicate]

There is go-eval from the same author as igo. It’s an improvement on the old exp/eval package. However, I was not able to import packages due to missing symbols, which is probably the reason for igo not supporting the import statement. It’s probably the best to go with compile/execute software as seen on play.golang.org.

Cmd folder for organizing project files in Go

So what about this ‘cmd’ folder? The post has already made it clear, to summarise: It’s not a magic nor standard in Go. It’s just a convention. You can have multiple binaries when putting them into a sub-folder which is not possible in the root folder. By making you taking your binary as a client … Read more

Why doesn’t Go have a function to calculate the absolute value of integers?

From Go’s FAQ, The standard library’s purpose is to support the runtime, connect to the operating system, and provide key functionality that many Go programs require, such as formatted I/O and networking. It also contains elements important for web programming, including cryptography and support for standards like HTTP, JSON, and XML. There is no clear … Read more

How do you make a function accept multiple types?

You can use interface types as arguments, in which case you can call the function with any type that implements the given interface. In Go types automatically implement any interfaces if they have the interface’s methods. So if you want to accept all possible types, you can use empty interface (interface{}) since all types implement … Read more