# 常數

V程式語言使用`const關鍵字來宣告常數，必須在模組層級作用域中函式外部宣告常數。`

`常數的命名約定為`snake\_case`，僅能用小寫字母、數字及底線且必須以小寫字母作起始。`

```v
const app_version = '1.0.0'
```

當要宣告多個常數時，可使用括弧形成常數群組。

```v
const (
    app_version = '1.0.0'
    max_size = 1024
)
```

V程式語言允許用`struct`型別宣告常數：

```v
struct Point {
mut:
    x f32
    y f32
    z f32
}

const origin = Point {
    x: 0.0
    y: 0.0,
    z: 0.0
}
```

可以使用函式運算後的返回值來宣告常數：

```v
module main

struct Point {
mut:
	x f32
	y f32
	z f32
}

fn shift_y(p Point, offset f32) Point {
	return Point{
		x: p.x
		y: p.y + offset
		z: p.z
	}
}

const (
	origin = Point{
		x: 0.0
		y: 0.0
		z: 0.0
	}

	origin_y = shift_y(origin, 2.5)
)

fn main() {
	print(origin_y)
}
```

在函式內部宣告常數會導致編譯錯誤：

```v
module main

fn main() {
	const app_version = '1.0.0'
	// error: const can only be defined at the top level (outside of functions)
	print(app_version)
}
```

模組中引用常數必需添加模組名稱前綴來標識：

```v
module bar

const foo = 1

pub fn run() {
    print(bar.foo)
}
```


---

# 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://docs.vlang.tw/basics/chang-shu.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.
