Go-compiled binary won’t run in an alpine docker container on Ubuntu host

By default, if using the net package a build will likely produce a binary with some dynamic linking, e.g. to libc. You can inspect dynamically vs. statically link by viewing the result of ldd output.bin There are two solutions I’ve come across: Disable CGO, via CGO_ENABLED=0 Force the use of the Go implementation of net … Read more

Golang parse a json with DYNAMIC key [duplicate]

I believe you want something like this: type Person struct { Name string `json:”name”` Age int `json:”age”` } type Info map[string]Person Then, after decoding this works: fmt.Printf(“%s: %d\n”, info[“bvu62fu6dq”].Name, info[“bvu62fu6dq”].Age) Full example: http://play.golang.org/p/FyH-cDp3Na

What does a function without body mean?

The function is defined here: // startTimer adds t to the timer heap. //go:linkname startTimer time.startTimer func startTimer(t *timer) { if raceenabled { racerelease(unsafe.Pointer(t)) } addtimer(t) } Function declarations: A function declaration may omit the body. Such a declaration provides the signature for a function implemented outside Go, such as an assembly routine. Not every … Read more

Go gin framework CORS

FWIW, this is my CORS Middleware that works for my needs. func CORSMiddleware() gin.HandlerFunc { return func(c *gin.Context) { c.Writer.Header().Set(“Access-Control-Allow-Origin”, “*”) c.Writer.Header().Set(“Access-Control-Allow-Credentials”, “true”) c.Writer.Header().Set(“Access-Control-Allow-Headers”, “Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With”) c.Writer.Header().Set(“Access-Control-Allow-Methods”, “POST, OPTIONS, GET, PUT”) if c.Request.Method == “OPTIONS” { c.AbortWithStatus(204) return } c.Next() } }

How to get the name of a function in Go?

I found a solution: package main import ( “fmt” “reflect” “runtime” ) func foo() { } func GetFunctionName(i interface{}) string { return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() } func main() { // This will print “name: main.foo” fmt.Println(“name:”, GetFunctionName(foo)) }

Converting map to struct

The simplest way would be to use https://github.com/mitchellh/mapstructure import “github.com/mitchellh/mapstructure” mapstructure.Decode(myData, &result) If you want to do it yourself, you could do something like this: http://play.golang.org/p/tN8mxT_V9h func SetField(obj interface{}, name string, value interface{}) error { structValue := reflect.ValueOf(obj).Elem() structFieldValue := structValue.FieldByName(name) if !structFieldValue.IsValid() { return fmt.Errorf(“No such field: %s in obj”, name) } if !structFieldValue.CanSet() … Read more

tech