Is it OK to leave a channel open?

It’s OK to leave a Go channel open forever and never close it. When the channel is no longer used, it will be garbage collected. Note that it is only necessary to close a channel if the receiver is looking for a close. Closing the channel is a control signal on the channel indicating that … Read more

How to set default values in Go structs

One possible idea is to write separate constructor function //Something is the structure we work with type Something struct { Text string DefaultText string } // NewSomething create new instance of Something func NewSomething(text string) Something { something := Something{} something.Text = text something.DefaultText = “default text” return something }

How does Go update third-party packages?

go get will install the package in the first directory listed at GOPATH (an environment variable which might contain a colon separated list of directories). You can use go get -u to update existing packages. You can also use go get -u all to update all packages in your GOPATH For larger projects, it might … Read more

Organizing a multiple-file Go project [closed]

I would recommend reviewing this page on How to Write Go Code It documents both how to structure your project in a go build friendly way, and also how to write tests. Tests do not need to be a cmd using the main package. They can simply be TestX named functions as part of each … Read more

Convert byte slice to io.Reader

To get a type that implements io.Reader from a []byte slice, you can use bytes.NewReader in the bytes package: r := bytes.NewReader(byteData) This will return a value of type bytes.Reader which implements the io.Reader (and io.ReadSeeker) interface. Don’t worry about them not being the same “type”. io.Reader is an interface and can be implemented by … 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

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