# Go 语言常量
常量类似标识符,一旦定义便不被修改,可以是 bool,number 或 string
# 定义
/* hello.go */ | |
package main | |
import ( | |
"fmt" | |
"unsafe" | |
) | |
const SERVER_IP string = "127.0.0.1" | |
const SERVER_PORT int = 9527 | |
const SERVER_HOST = "jalen.site" | |
const IP, PORT = "192.168.0.1", 3306 | |
const ( | |
Unknown = 0 | |
Female = 1 | |
Male = 2 | |
) | |
const ( | |
name = "Jalen" | |
age = 12 | |
phoneNumber = "18888888888" //11 | |
phoneNumberLength = len(phoneNumber) | |
// 字符串类型在 go 里对应一个结构体,该结构体有两个域,第一个域是指向该字符串的指针, | |
// 第二个域是字符串的长度,每个域占 8 个字节,但是并不包含指针指向的字符串的内容 | |
nameSize = unsafe.Sizeof(name) // 输出 16 | |
) | |
const ( | |
num1 = iota //0 | |
num2 = iota //1 | |
num3 = iota //2 | |
) | |
const ( | |
num4 = iota //0 | |
num5 //1 | |
num6 //2 | |
num7 = iota //3 | |
) | |
const ( | |
num8 = iota //0 | |
num9 = 20 //20 | |
num10 = 90 //90 | |
num11 //90 | |
num12 //90 | |
num13 = iota //5 | |
num14 = "haha" //haha | |
num15 //haha | |
num16 = iota //8 | |
) | |
const ( | |
i = 1 << iota //1 左移 0 位,不变,=1 | |
j = 3 << iota //3 左移 1 位,*2^1,=6 | |
k //3 左移 2 位,*2^2, =12 | |
l //3 左移 3 位,*2^3, =24 | |
) | |
const ( | |
m = 1 //1 | |
n //1 | |
o //1 | |
p //1 | |
) | |
func main() { | |
fmt.Println("测试常量赋值") | |
} |