> For the complete documentation index, see [llms.txt](https://go.netdpi.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://go.netdpi.net/variable/scope.md).

# 04.02-Scope（變數的作用範圍）

回到我們在本章一開始所討論的程式：

```go
package main

import "fmt"

func main() {
    var x string = "Hello World"
    fmt.Println(x)
}
```

用另一種方式來寫這個程式，類似這樣：

```go
package main

import "fmt"

var x string = "Hello World"

func main() {
    fmt.Println(x)
}
```

注意，我們將變數移到 main 函式的外部，這表示其它函式都可以存取這個變數：

```go
var x string = "Hello World"

func main() {
    fmt.Println(x)
}

func f() {
    fmt.Println(x)
}
```

`f` 函式現在可以存取 `x` 變數了，現在我們改成這樣：

```go
func main() {
    var x string = "Hello World"
    fmt.Println(x)
}

func f() {
    fmt.Println(x)
}
```

如果你執行這個程式，你應該會看到一個錯誤：

```
.\main.go:11: undefined: x
```

編譯器告訴你 `f` 函式裡的 `x` 變數不存在，它只有在 `main` 函式裡面，你可以使用 `x` 的範圍就稱為這個變數的 scope。依據語言的規範，Go 是以區塊範圍的區隔（“Go is lexically scoped using blocks”）。基本上，這表示變數存在於最接近的大括號裡 `{` `}` （一個區塊），包含任何巢狀的大括號，但不在大括號外面。一開始會對 scope 有點困惑，不過只要我們多看些 Go 的範例，應該就會比較清楚了。


---

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

```
GET https://go.netdpi.net/variable/scope.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.
