What is a rune?

Rune literals are just 32-bit integer values (however they’re untyped constants, so their type can change). They represent unicode codepoints. For example, the rune literal ‘a’ is actually the number 97. Therefore your program is pretty much equivalent to: It should be obvious, if you were to look at the Unicode mapping, which is identical … Read more

GO language: fatal error: all goroutines are asleep – deadlock

Go program ends when the main function ends. From the language specification Program execution begins by initializing the main package and then invoking the function main. When that function invocation returns, the program exits. It does not wait for other (non-main) goroutines to complete. Therefore, you need to wait for your goroutines to finish. The … Read more

Categories go Tags

How to uninstall Go?

72 You might try then remove any mention of go in e.g. your ~/.bashrc; then you need at least to logout and login. However, be careful when doing that. You might break your system badly if something is wrong. PS. I am assuming a Linux or POSIX system.

Categories go Tags

Is there a foreach loop in Go?

A “for” statement with a “range” clause iterates through all entries of an array, slice, string or map, or values received on a channel. For each entry it assigns iteration values to corresponding iteration variables and then executes the block. As an example: If you don’t care about the index, you can use _: The … Read more

Go build: “Cannot find package” (even though GOPATH is set)

It does not work because your foobar.go source file is not in a directory called foobar. go build and go install try to match directories, not source files. Set $GOPATH to a valid directory, e.g. export GOPATH=”$HOME/go” Move foobar.go to $GOPATH/src/foobar/foobar.go and building should work just fine. Additional recommended steps: Add $GOPATH/bin to your $PATH by: PATH=”$GOPATH/bin:$PATH” Move main.go to a subfolder of $GOPATH/src, e.g. $GOPATH/src/test go install test should now create an executable in $GOPATH/bin that can be called by … Read more

cannot download, $GOPATH not set

[Update: as of Go 1.8, GOPATH defaults to $HOME/go, but you may still find this useful if you want to understand the GOPATH layout, customize it, etc.] The official Go site discusses GOPATH and how to lay out a workspace directory. export GOPATH=”$HOME/your-workspace-dir/” — run it in your shell, then add it to ~/.bashrc or … Read more

Contains method for a slice

Mostafa has already pointed out that such a method is trivial to write, and mkb gave you a hint to use the binary search from the sort package. But if you are going to do a lot of such contains checks, you might also consider using a map instead. It’s trivial to check if a specific map … Read more

Categories go Tags