Looking for go Answers? Try Ask4KnowledgeBase
Looking for go Keywords? Try Ask4Keywords

Go Reading and writing to a file using ioutil


A simple program that writes "Hello, world!" to test.txt, reads back the data, and prints it out. Demonstrates simple file I/O operations.

package main

import (

func main() {
    hello := []byte("Hello, world!")

    // Write `Hello, world!` to test.txt that can read/written by user and read by others 
    err := ioutil.WriteFile("test.txt", hello, 0644)
    if err != nil {

    // Read test.txt
    data, err := ioutil.ReadFile("test.txt")
    if err != nil {

    // Should output: `The file contains: Hello, world!`
    fmt.Println("The file contains: " + string(data))