/////////////////////////////////
// Writing Bytes to Files
// Go Playground: https://play.golang.org/p/Zc3KDG7kYvt
/////////////////////////////////

package main

import (
    "io/ioutil"
    "log"
    "os"
)

func main() {

    // opening the file in write-only mode if the file exists and then it truncates the file.
    // if the file doesn't exist it creates the file with 0644 permissions
    file, err := os.OpenFile(
        "b.txt",
        os.O_WRONLY|os.O_TRUNC|os.O_CREATE,
        0644,
    )
    // error handling
    if err != nil {
        log.Fatal(err)
    }
    // defer closing the file
    defer file.Close()

    // WRITING BYTES TO FILE

    byteSlice := []byte("I learn Golang! δΌ ")   // converting a string to a bytes slice
    bytesWritten, err := file.Write(byteSlice) // writing bytes to file.
    // It returns the no. of bytes written and an error value
    // error handling
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("Bytes written: %d\n", bytesWritten) // => 2019/10/21 16:26:16 Bytes written: 19

    // WRITING BYTES TO FILE USING ioutil.WriteFile()

    // ioutil.WriteFile() handles creating, opening, writing a slice of bytes and closing the file.
    // if the file doesn't exist WriteFile() creates it
    // and if it already exists the function will truncate it before writing to file.

    bs := []byte("Go Programming is cool!")
    err = ioutil.WriteFile("c.txt", bs, 0644)
    // error handling
    if err != nil {
        log.Fatal(err)
    }
}