学习Golang中的字母和常量(附代码示例)

40 阅读1分钟

学习Golang中的字母和常量

字符

在Go中,值可以是很多东西。仅举几例,值可以是数字(如109),或用引号包裹的文本(如 "Hello world")。

字符是写在源代码中的值。比如说:

fmt.Println("Hello, world!")       // String literal
fmt.Println(42)                    // Integer literal
fmt.Println(3.141592653589793238)  // Floating point literal

常量

除了字面价值(即直接在源代码中表达的价值),Go还允许 "命名的价值",即用一个名称来识别的价值。这些命名的值被称为 "常量",因为名称所代表的值在程序的整个运行期间保持不变。

例子:

package main

import (
  "fmt"
)

func main() {
  const inchesPerFeet = 12
  fmt.Println("There are", inchesPerFeet, "inches in a foot.")
}

这个程序打印出There are 12 inches in a foot.