> For the complete documentation index, see [llms.txt](https://wskdsgcf.gitbook.io/mastering-go-zh-cn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wskdsgcf.gitbook.io/mastering-go-zh-cn/08.0/08.13.md).

# 08.13 文件权限

`Unix`系统编程中的一个热门话题是`Unix`文件权限。在本节中，假设你有足够的`Unix`权限，你将学习如何输出任意文件的权限！程序名为`permission.go`，分为三部分。

`permission.go`的第一部分代码如下：

```go
package main

import (
    "fmt"
    "os"
)
```

`permission.go`的第二部分代码如下：

```go
func main() {
    arguments := os.Args
    if len(arguments) == 1 {
        fmt.Printf("usage:permissions filename\n")
        return
    }
```

最后一部分代码如下：

```go
    filename := arguments[1]
    info, _ := os.Stat(filename)
    mode := info.Mode()
    fmt.Println(filename, "mode is", mode.String()[1:10])
}
```

`os.Stat(filename)`调用返回一个大结构体，其中包含了许多数据。因为我们只对文件的权限感兴趣，我们调用`Mode()`方式并打印输出。实际上，我们通过`mode.String()[1:10]`打印了输出的一部分，因为它即是我们感兴趣的那部分。

执行`permission.go`将产生如下输出：

```
$ go run permission.go /tmp/adobegc.log
/tmp/adobegc.log mode is rw-rw-rw-
$ go run permissions.go /dev/random
/dev/random mode is crw-rw-rw
```

`ls(1)`输出可以验证`permission.go`的正确性：

```
$ ls -l /dev/random /tmp/adobegc.log
crw-rw-rw- 1 root wheel 14,   0 Jan 8 20:24 /dev/random
-rw-rw-rw- 1 root wheel  583454 Jan 16 19:12 /tmp/adobegc.log
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://wskdsgcf.gitbook.io/mastering-go-zh-cn/08.0/08.13.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
