在 Go 程式設計中,os.FileMode 類型表示檔案權限和屬性。然而,許多範例忽略了用於檔案建立或修改的 os.FileMode 的正確實例化。本文探討如何準確實例化 os.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_READ << OS_USER_SHIFT OS_USER_W = OS_WRITE << OS_USER_SHIFT OS_USER_X = OS_EX << OS_USER_SHIFT OS_USER_RW = OS_USER_R | OS_USER_W OS_USER_RWX = OS_USER_RW | OS_USER_X OS_GROUP_R = OS_READ << OS_GROUP_SHIFT OS_GROUP_W = OS_WRITE << OS_GROUP_SHIFT OS_GROUP_X = OS_EX << OS_GROUP_SHIFT OS_GROUP_RW = OS_GROUP_R | OS_GROUP_W OS_GROUP_RWX = OS_GROUP_RW | OS_GROUP_X OS_OTH_R = OS_READ << OS_OTH_SHIFT OS_OTH_W = OS_WRITE << OS_OTH_SHIFT OS_OTH_X = OS_EX << OS_OTH_SHIFT OS_OTH_RW = OS_OTH_R | OS_OTH_W OS_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 )
現在,您可以明確指定您想要的權限:
var dir_file_mode os.FileMode dir_file_mode = os.ModeDir | (OS_USER_RWX | OS_ALL_R) os.MkdirAll(dir_str, dir_file_mode)
以上是如何在 Go 中準確地實例化 `os.FileMode` 以進行檔案建立和更新?的詳細內容。更多資訊請關注PHP中文網其他相關文章!