Application auto build versioning

The Go linker (go tool link) has an option to set the value of an uninitialised string variable: -X importpath.name=value Set the value of the string variable in importpath named name to value. Note that before Go 1.5 this option took two separate arguments. Now it takes one argument split on the first = sign. … Read more

golang how does the rune() function work

rune is a type in Go. It’s just an alias for int32, but it’s usually used to represent Unicode points. rune() isn’t a function, it’s syntax for type conversion into rune. Conversions in Go always have the syntax type() which might make them look like functions. The first bit of code fails because conversion of … Read more

Prevent the main() function from terminating before goroutines finish in Golang

Simplest, cleanest and “scalable” way to do it is to use a sync.WaitGroup: var wg = &sync.WaitGroup{} func printElo() { defer wg.Done() fmt.Printf(“Elo\n”) } func printHello() { defer wg.Done() fmt.Printf(“Hello\n”) } func main() { fmt.Printf(“This will print.”) i := 0 for i < 10 { wg.Add(1) go printElo() wg.Add(1) go printHello() i++ } wg.Wait() } … Read more

What does (*[1

*[1 << 30]C.YourType doesn’t do anything itself, it’s a type. Specifically, it’s a pointer to an array of size 1 << 30, of C.YourType values. The size is arbitrary, and only represents an upper bound that needs to be valid on the host system. What you’re doing in the third expression is a type conversion. … Read more

How does Go perform arithmetic on constants?

Short summary (TL;DR) is at the end of the answer. Untyped arbitrary-precision constants don’t live at runtime, constants live only at compile time (during the compilation). That being said, Go does not have to represent constants with arbitrary precision at runtime, only when compiling your application. Why? Because constants do not get compiled into the … Read more

Why does append modify passed slice

Performance is the big reason. Creating a new slice and copying all the elements over to it is expensive, so the slice code doesn’t copy without good reason. However, if you exceed the slice’s capacity, it grows by a suitable amount by copying the underlying slice. That means that the slice that’s returned from append … Read more

Why does fmt.Println inside a goroutine not print a line?

Your program will exit when the main() function finishes. This is likely to happen before your goroutine has time to run and print its output. One option would be to have the main goroutine block reading from a channel, and have the goroutine write to the channel when it has completed its work.

How can I pass a slice as a variadic input?

The Go Programming Language Specification Passing arguments to … parameters If f is variadic with final parameter type …T, then within the function the argument is equivalent to a parameter of type []T. At each call of f, the argument passed to the final parameter is a new slice of type []T whose successive elements … Read more