Type converting slices of interfaces

In Go, there is a general rule that syntax should not hide complex/costly operations. Converting a string to an interface{} is done in O(1) time. Converting a []string to an interface{} is also done in O(1) time since a slice is still one value. However, converting a []string to an []interface{} is O(n) time because … Read more

How to run test cases in a specified file?

There are two ways. The easy one is to use the -run flag and provide a pattern matching names of the tests you want to run. Example: $ go test packageName -run NameOfTest See the docs for more info. Note that the -run flag may also run other tests if they contain the string NameOfTest, … Read more

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: package main import “fmt” func SwapRune(r rune) rune { switch { case 97 <= r && … Read more

Why would I make() or new()?

Go has multiple ways of memory allocation and value initialization: &T{…}, &someLocalVar, new, make Allocation can also happen when creating composite literals. new can be used to allocate values such as integers, &int is illegal: new(Point) &Point{} // OK &Point{2, 3} // Combines allocation and initialization new(int) &int // Illegal // Works, but it is … Read more

How to get the directory of the currently running file?

EDIT: As of Go 1.8 (Released February 2017) the recommended way of doing this is with os.Executable: func Executable() (string, error) Executable returns the path name for the executable that started the current process. There is no guarantee that the path is still pointing to the correct executable. If a symlink was used to start … Read more

How to handle configuration in Go [closed]

The JSON format worked for me quite well. The standard library offers methods to write the data structure indented, so it is quite readable. See also this golang-nuts thread. The benefits of JSON are that it is fairly simple to parse and human readable/editable while offering semantics for lists and mappings (which can become quite … Read more

How can I read from standard input in the console?

I’m not sure what’s wrong with the block reader := bufio.NewReader(os.Stdin) fmt.Print(“Enter text: “) text, _ := reader.ReadString(‘\n’) fmt.Println(text) As it works on my machine. However, for the next block you need a pointer to the variables you’re assigning the input to. Try replacing fmt.Scanln(text2) with fmt.Scanln(&text2). Don’t use Sscanln, because it parses a string … Read more

Removing packages installed with go get

It’s safe to just delete the source directory and compiled package file. Find the source directory under $GOPATH/src and the package file under $GOPATH/pkg/<architecture>, for example: $GOPATH/pkg/windows_amd64.

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 … Read more

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