Registering packages in Go without cyclic dependency

The standard library solves this problem in multiple ways: 1) Without a “Central” Registry Example of this is the different hash algorithms. The crypto package just defines the Hash interface (the type and its methods). Concrete implementations are in different packages (actually subfolders but doesn’t need to be) for example crypto/md5 and crypto/sha256. When you … 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

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)) }

Slice chunking in Go

You don’t need to make new slices, just append slices of logs to the divided slice. http://play.golang.org/p/vyihJZlDVy var divided [][]string chunkSize := (len(logs) + numCPU – 1) / numCPU for i := 0; i < len(logs); i += chunkSize { end := i + chunkSize if end > len(logs) { end = len(logs) } divided … Read more

Go Error Handling Techniques [closed]

Your code is idiomatic and in my opinion it is the best practice available. Some would disagree for sure, but I would argue that this is the style seen all over the standard libraries in Golang. In other words, Go authors write error handling in this way.

Why a generic can’t be assigned to another even if their type arguments can?

Instantiating a generic type with different type arguments produces two new different named types. Note that every time you supply a type argument, including in function arguments or return types, you are instantiating the generic type: // Props is instantiated with type argument ‘Generic’ func Problem() Props[Generic] { return ExampleProps } Therefore Props[Example] is just … Read more

Golang converting float64 to int error

You need to understand something: 100.55 is a decimal number (presented in decimal radix). 100.55 in decimal is a finite number and is exactly this: 100.55. Computers in general store numbers in binary representation. The number 100.55 cannot be represented with a finite binary number: 100.55 is an infinite number in binary representation (same reason … Read more