> 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/constant.md).

# 04.03-常數（Constant）

Go 也支援常數，常數基本上是值無法更動的變數。建立常數的方式與變數一樣，但是不需使用 `var` 關鍵字，而是使用 `const` 關鍵字：

```go
package main

import "fmt"

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

這個：

```
    const x string = "Hello World"
    x = "Some other string"
```

結果在編譯時會發生錯誤：

```
.\main.go:7: cannot assign to x
```

在程式裡，常數是重新使用一般值的一種好方法，因為可以不用每次都將值寫出來，例如：在 `math` package 的 `Pi` 就是定義為常數。


---

# 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/constant.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.
