Constructors in Go

There are some equivalents of constructors for when the zero values can’t make sensible default values or for when some parameter is necessary for the struct initialization. Supposing you have a struct like this : type Thing struct { Name string Num int } then, if the zero values aren’t fitting, you would typically construct … Read more

Converting Go struct to JSON

You need to export the User.name field so that the json package can see it. Rename the name field to Name. package main import ( “fmt” “encoding/json” ) type User struct { Name string } func main() { user := &User{Name: “Frank”} b, err := json.Marshal(user) if err != nil { fmt.Println(err) return } fmt.Println(string(b)) … Read more

How to delete an element from a Slice in Golang

Order matters If you want to keep your array ordered, you have to shift all of the elements at the right of the deleting index by one to the left. Hopefully, this can be done easily in Golang: func remove(slice []int, s int) []int { return append(slice[:s], slice[s+1:]…) } However, this is inefficient because you … Read more

What does an underscore in front of an import statement mean?

It’s for importing a package solely for its side-effects. From the Go Specification: To import a package solely for its side-effects (initialization), use the blank identifier as explicit package name: import _ “lib/math” In sqlite3 In the case of go-sqlite3, the underscore import is used for the side-effect of registering the sqlite3 driver as a … Read more

List directory in Go

You can try using the ReadDir function in the io/ioutil package. Per the docs: ReadDir reads the directory named by dirname and returns a list of sorted directory entries. The resulting slice contains os.FileInfo types, which provide the methods listed here. Here is a basic example that lists the name of everything in the current … Read more

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

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