ioutils.WriteFile()不尊重权限

I'm trying to use ioutils.WriteFile() but for some reason it's ignoring the 0777 permissions I'm giving it.

package main

import (
    "io/ioutil"
    "os"
)

func main() {

    // normal permissions
    if err := ioutil.WriteFile("cant-touch-this-0644", []byte{}, 0644); err != nil {
            panic(err)
    }


    // full permissions
    if err := ioutil.WriteFile("cant-touch-this-0777", []byte{}, 0777); err != nil {
            panic(err)
    }


    // normal permissions + chmod to full
    if err := ioutil.WriteFile("cant-touch-this-mixed", []byte{}, 0755); err != nil {
            panic(err)
    }

    if err := os.Chmod("cant-touch-this-mixed", 0777); err != nil {
            panic(err)
    }
}

The output I get from this is:

$ ls -l
-rw-r--r--  1 edson edson    0 May  9 17:19  cant-touch-this-0644
-rwxr-xr-x  1 edson edson    0 May  9 17:19  cant-touch-this-0777
-rwxrwxrwx  1 edson edson    0 May  9 17:19  cant-touch-this-mixed

Which means:

  • The first scenario (0644) worked
  • The second (0777) was ignored
  • The only way I can get full 0777 permissions is by using a os.Chmod (like in the third scenario)

What am I doing wrong?

Permission deny.

Try:

sudo go run main.go

Or change user to root and then execute.

How to change user to root:

sudo su -