Go语言 Day08 Summary part1

文章展示了如何使用Go语言进行文件写入,包括WriteFile函数,创建文件并写入数据,以及使用bufio进行缓冲写入。同时,还介绍了如何创建行过滤器程序,从标准输入读取数据并处理。另外,文章还讨论了filepath包在处理跨平台路径时的作用。
摘要由CSDN通过智能技术生成

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值