P
P
progerstas2020-11-13 12:18:46
go
progerstas, 2020-11-13 12:18:46

How to write an array of bytes to a file in golang?

Tried to do so

permissions := 0644 // or whatever you need
byteArray := []byte("to be written to a file\n")
err := ioutil.WriteFile("file.txt", byteArray, permissions)
if err != nil { 
    // handle error
}


mistake:
./main.go:192:26: cannot use permissions (type int) as type os.FileMode in argument to ioutil.WriteFile

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Samsonov, 2020-11-13
@progerstas

You need to cast permissions to type os.FileMode

permissions := 0644 // or whatever you need
byteArray := []byte("to be written to a file\n")
err := ioutil.WriteFile("file.txt", byteArray, os.FileMode(permissions))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question