1.Writing Files
Writing files in Go follows similar patterns to the ones we saw earlier for reading.
[maxwell@oracle-db-19c Day08]$ vim writing-files.go
[maxwell@oracle-db-19c Day08]$ go run writing-files.go
wrote 4 bytes
wrote 7 bytes
wrote 9 bytes
[maxwell@oracle-db-19c Day08]$ cat writing-files.go
package main
import (
"bufio"
"fmt"
"os"
)
func check(e error){
if e != nil {
panic(e)
}
}
func main() {
d1 := []byte("hello\ngo\n")
err := os.WriteFile("/tmp/dat1", d1, 0644)
check(err)
f, err := os.Create("/tmp/dat2")
defer f.Close()
d2 := []byte{115, 111, 109, 10}
n2, err := f.Write(d2)
check(err)
fmt.Printf("wrote %d bytes\n", n2)
n3, err := f.WriteString("writes\n")
check(err)
fmt.Printf("wrote %d bytes\n", n3)
f.Sync()
w := bufio.NewWriter(f)
n4, err := w.WriteString("buffered\n")
check(err)
fmt.Printf("wrote %d bytes\n", n4)
w.Flush()
}
[maxwell@oracle-db-19c Day08]$ cat /tmp/dat1
hello
go
[maxwell@oracle-db-19c Day08]$ cat /tmp/dat2
som
writes
buffered
[maxwell@oracle-db-19c Day08]$
2. Line Filters
A line filter is a common type of program that reads input on stdin, processes it, and then prints some derived result to stdout. grep
and sed
are common line filters.
[maxwell@oracle-db-19c Day08]$ vim line-filters.go
[maxwell@oracle-db-19c Day08]$ cat line-filters.go
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main(){
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan(){
ucl := strings.ToUpper(scanner.Text())
fmt.Println(ucl)
}
if err := scanner.Err();err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
}
[maxwell@oracle-db-19c Day08]$ echo 'hello' > /tmp/lines
[maxwell@oracle-db-19c Day08]$ echo 'filter' >> /tmp/lines
[maxwell@oracle-db-19c Day08]$
[maxwell@oracle-db-19c Day08]$ cat /tmp/lines | go run line-filters.go
HELLO
FILTER
[maxwell@oracle-db-19c Day08]$
3. File Paths
The filepath
package provides functions to parse and construct file paths in a way that is portable between operating systems; dir/file
on Linux vs. dir\file
on Windows, for example.
[maxwell@oracle-db-19c Day08]$ vim file-paths.go
[maxwell@oracle-db-19c Day08]$ go run file-paths.go
p: dir1/dir2/filename
dir1/filename
dir1/filename
Dir(p): dir1/dir2
Base(p): filename
false
true
.json
config
t/file
../c/t/file
[maxwell@oracle-db-19c Day08]$ cat file-paths.go
package main
import (
"fmt"
"path/filepath"
"strings"
)
func main(){
p := filepath.Join("dir1", "dir2", "filename")
fmt.Println("p:", p)
fmt.Println(filepath.Join("dir1//","filename"))
fmt.Println(filepath.Join("dir1/../dir1", "filename"))
fmt.Println("Dir(p):", filepath.Dir(p))
fmt.Println("Base(p):", filepath.Base(p))
fmt.Println(filepath.IsAbs("dir/file"))
fmt.Println(filepath.IsAbs("/dir/file"))
filename := "config.json"
ext := filepath.Ext(filename)
fmt.Println(ext)
fmt.Println(strings.TrimSuffix(filename,ext))
rel, err := filepath.Rel("a/b", "a/b/t/file")
if err != nil{
panic(err)
}
fmt.Println(rel)
rel, err = filepath.Rel("a/b", "a/c/t/file")
if err != nil {
panic(err)
}
fmt.Println(rel)
}
[maxwell@oracle-db-19c Day08]$
4. Directories
Go has several useful functions for working with directories in the file system.