# 12.6.1 获取域名的 NS记录

一个 DNS 请求常见的作用是找到域名的 **名称服务**，它们被存储在域名的 **NS 记录**中。这个功能将会在 `NSrecords.go` 的代码中介绍。

`NSrecords.go` 的代码将由俩部分展示。第一部分如下：

```go
package main

import(
    "fmt"
    "net"
    "os"
)

func main() {
    arguments := os.Args
    if len(arguments) == 1 {
        fmt.Println("Need a domain name!")
        return
    }
```

在这部分，您要检查是否至少有一个命令行参数，以便有可以运行的参数。

`NSrecords.go` 的剩余代码如下：

```go
    domain := arguments[1]
    NSs, err := net.LookupNS(domain)
    if err != nil {
        fmt.Println(err)
        return
    }

    for _, NS := range NSs {
        fmt.Println(NS.Host)
    }
}
```

`net.LookupNS()` 函数做了所有的工作，它将域名的 **NS 记录**作为 `net.NS` 类型的 slice 变量返回。这是打印 slice 的每个 `net.NS` 元素的 `Host` 字段的原因。

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

```
$ go run NSrecords.go mtsoukalos.eu
ns5.linode.com.
ns4.linode.com.
ns1.linode.com.
ns2.linode.com.
ns3.linode.com.
$ go run NSrecords.go www.mtsoukalos.eu
lookup www.mtsoukalos.eu on 8.8.8.8:53: no such host
```

您可以用 `host(1)` 工具验证上面输出的正确性：

```
$ host -t ns www.mtsoukalos.eu
www.mtsoukalos.eu has no NS record
$ host -t ns mtsoukalos.eu
mtsoukalos.eu name server ns3.linode.com.
mtsoukalos.eu name server ns1.linode.com.
mtsoukalos.eu name server ns4.linode.com.
mtsoukalos.eu name server ns2.linode.com.
mtsoukalos.eu name server ns5.linode.com.
```


---

# Agent Instructions: 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:

```
GET https://wskdsgcf.gitbook.io/mastering-go-zh-cn/12.0/12.6/12.6.1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
