Break up go project into subfolders

I recently achieved this by using go modules. Golang introduced preliminary opt-in support for modules as of go v1.11.1 which is intended to completely remove the, frankly, absurd $GOPATH necessity. Not only can you now have versioned dependencies in any normal directory such as ~/development, but you can basically have something that looks like namespaces … Read more

Is there a package to marshal in and out of x-www-form-urlencoding in golang

gorilla/schema is popular and well maintained: e.g. func FormHandler(w http.RequestWriter, r *http.Request) { err := r.ParseForm() if err != nil { // handle error } person := new(Person) // Person being a struct type decoder := schema.NewDecoder() err = decoder.Decode(person, r.Form) if err != nil { // handle error } } goforms is also an … Read more

Behavior of sleep and select in go

That’s a very interesting question, so I did cd into my Go source to start looking. time.Sleep time.Sleep is defined like this: // src/time/sleep.go // Sleep pauses the current goroutine for at least the duration d. // A negative or zero duration causes Sleep to return immediately. func Sleep(d Duration) No body, no definition in … Read more

why dash is used in condition Go templates

Dash removes the spaces from the output on the side it appears in the template: https://golang.org/pkg/text/template/#hdr-Text_and_spaces {{- if …}} The above will remove all spaces that appear before the if statement, so if the result of if prints something, it’ll be right after the last piece of text without any spaces.

How to get intersection of two slice in golang?

How do I get the intersection between two arrays as a new array? Simple Intersection: Compare each element in A to each in B (O(n^2)) Hash Intersection: Put them into a hash table (O(n)) Sorted Intersection: Sort A and do an optimized intersection (O(n*log(n))) All of which are implemented here https://github.com/juliangruber/go-intersect/blob/master/intersect.go

Golang floating point precision float32 vs float64

Using math.Float32bits and math.Float64bits, you can see how Go represents the different decimal values as a IEEE 754 binary value: Playground: https://play.golang.org/p/ZqzdCZLfvC Result: float32(0.1): 00111101110011001100110011001101 float32(0.2): 00111110010011001100110011001101 float32(0.3): 00111110100110011001100110011010 float64(0.1): 0011111110111001100110011001100110011001100110011001100110011010 float64(0.2): 0011111111001001100110011001100110011001100110011001100110011010 float64(0.3): 0011111111010011001100110011001100110011001100110011001100110011 If you convert these binary representation to decimal values and do your loop, you can see that for float32, the … Read more

When to use a buffered channel?

To give a single, slightly-more-concrete use case: Suppose you want your channel to represent a task queue, so that a task scheduler can send jobs into the queue, and a worker thread can consume a job by receiving it in the channel. Suppose further that, though in general you expect each job to be handled … Read more

How do you determine if a variable is a slice or array?

Have a look at the reflect package. Here is a working sample for you to play with. package main import “fmt” import “reflect” func main() { m := make(map[string]interface{}) m[“a”] = []string{“a”, “b”, “c”} m[“b”] = [4]int{1, 2, 3, 4} test(m) } func test(m map[string]interface{}) { for k, v := range m { rt := … Read more

Does a channel return two values?

The boolean variable ok returned by a receive operator indicates whether the received value was sent on the channel (true) or is a zero value returned because the channel is closed and empty (false). The for loop terminates when some other part of the Go program closes the fromServer or the fromUser channel. In that … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)