You are here
Home > golang

go-gossip

  • 格式化输出用 fmt.printf代表 pintfln

  • 写文件权限

    f, e = os.OpenFile(fname, os.O_APPEND, 0666) //打开文件
    
    文件存在
    panic: write ./t.txt: bad file descriptor
    
    goroutine 1 [running]:
    main.check(0x112e200, 0xc420072180)
        /golang/goproject/gofile/gofile.go:11 +0x4a
    main.main()
        /golang/goproject/gofile/gofile.go:38 +0x15a
    

    解决办法:

    f, e = os.OpenFile(fname, os.O_APPEND|os.O_WRONLY,os.ModeAppend)
    
    if f, err := os.OpenFile("./log.log", os.O_APPEND|os.O_WRONLY, os.ModeAppend); err != nil { /*[...]*/ }
    

    To explain, here is the linux documentation for open: http://man7.org/linux/man-pages/man2/openat.2.html :

    The argument flags must include one of the following access modes: O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read- only, write-only, or read/write, respectively.

    If you check /usr/local/go/src/syscall/zerrors_linux_amd64.go:660, you can see that:

    O_RDONLY                         = 0x0
    O_RDWR                           = 0x2
    O_WRONLY                         = 0x1
    

    So by default you get a read-only file descriptor.

发表评论

Top