Day 3 Challenge
Write a program in Go to create a file, write something in it then read the content back.
The code
You can see the coding replay here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main
import "fmt"
import "os"
func main(){
file, error := os.Create("/tmp/todo.list")
if error != nil {
fmt.Println("Error while creating a file. ", error)
} else {
_, writeErr := file.WriteString("Day 3 challenge.")
if writeErr != nil {
fmt.Println("Error while writing a file. ", writeErr)
}
file.Close()
}
readonlyFile, _ := os.Open("/tmp/todo.list")
buffer := make([]byte, 100)
count, _ := readonlyFile.Read(buffer)
s := string(buffer[:count])
fmt.Println("Content is: ", s)
readonlyFile.Close();
}