This one-liner assigns a string to a slice of bytes:

package main

func main() {

    var s string

    //...

    b := []byte(s)

    //...
}

I found this useful when using ioutil.WriteFile, which accepts a bytes slice as its data parameter:

WriteFile func(filename string, data []byte, perm os.FileMode) error

e.g.:

ioutil.WriteFile(filePath, []byte(content), 0755)

Want to hire me?

I'm currently considering remote job opportunities.

I'm interested in Go and/or JavaScript senior software engineer positions.

Read more about me and if you're interested, get in touch.