Golang 寫檔案的許可權定義perm

2021-09-20 06:43:35 字數 1824 閱讀 3424

ioutil包中寫檔案的方法:

funcwritefile(filename string, data byte, perm os.filemode) error

關於許可權的定義,可以參考golang源**:\go\src\os\types.go

perm是filemode型別的:

typefilemode uint32

// the nine least-significant bits are the standard unix rwxrwxrwx permissions.

const(

modeperm filemode = 0777   // unix permission bits

filemode在所有的系統中定義都是相同的,常用的值定義如下:

const (

os_read = 04

os_write = 02

os_ex = 01

os_user_shift = 6

os_group_shift = 3

os_oth_shift = 0 

os_user_r = os_reados_user_rwx = os_user_rw | os_user_x

os_group_r = os_reados_group_rwx = os_group_rw | os_group_x

os_oth_r = os_reados_oth_rwx = os_oth_rw | os_oth_x

os_all_r = os_user_r | os_group_r | os_oth_r

os_all_w = os_user_w | os_group_w | os_oth_w

os_all_x = os_user_x | os_group_x | os_oth_x

os_all_rw = os_all_r | os_all_w

os_all_rwx = os_all_rw | os_group_x)

一般都檔案屬性標識如下: 

-rwxrwxrwx

第1位:檔案屬性,一般常用的是"-",表示是普通檔案;"d"表示是乙個目錄。

第2~4位:檔案所有者的許可權rwx (可讀/可寫/可執行)。

第5~7位:檔案所屬使用者組的許可權rwx (可讀/可寫/可執行)。

第8~10位:其他人的許可權rwx (可讀/可寫/可執行)。

在golang中,可以使用os.filemode(perm).string()來檢視許可權標識:

os.filemode(0777).string()    //返回 -rwxrwxrwx

os.filemode(0666).string()   //返回 -rw-rw-rw-

os.filemode(0644).string()   //返回 -rw-r--r--

0777表示:建立了乙個普通檔案,所有人擁有所有的讀、寫、執行許可權

0666表示:建立了乙個普通檔案,所有人擁有對該檔案的讀、寫許可權,但是都不可執行

0644表示:建立了乙個普通檔案,檔案所有者對該檔案有讀寫許可權,使用者組和其他人只有讀許可權,都沒有執行許可權

注意,golang中建立檔案指定許可權時,只能以"0***"的形式,不能省掉前面的"0",否則指定的許可權不是預期的。如:

os.filemode(777).string()   //返回 -r----x--x

os.filemode(666).string()   //返回 --w--wx-w-

os.filemode(644).string()   //返回 --w----r--

Golang寫檔案的坑

golang寫檔案一般使用os.openfile返回檔案指標的write方法或者writestring或者writeat方法,但是在使用這三個方法時候經常會遇到寫入的內容和實際內容有出入,因為這幾個函式採用的不是清空覆蓋的方式,有時字串或陣列長度和檔案內容不一致的時候只覆蓋了一部分,這就需要在呼叫o...

架構之路之shiro的許可權定義兩種方法

public anon static anon user edit perms user edit authc 在usercontroller方法上面加註解 requirespermissions user edit public string list model model 你有許可權看到此處 ...

改進版本號的精確資料許可權定義和實現

因為project實現上的某些小問題。為了達到方便實現如圖效果,對資料結構做了一點點的調整。新的資料結構例如以下圖 go 檢視 查詢全部角色的資料許可權 create view roledatapermit aswith list as select distinct g.id as dataid,...