https://haicoder.net/golang/golang-float.html
1.go语言常量
常量,就是程序编译阶段
就确定下来的值,而程序在运行时
则无法改变该值。g在Go程序里,常量可以是数值类型(包括整型、浮点型和复数类型)、布尔类型、字符串类型等。
1.1定义常量的语法
const
定义一个常量使用const关键字,语法格式如下
go
const constantName [type]= value
const:定义常量关键字
constName: 常量名称
type: 常量类型
value:常量值
//例子
package main
func main() {
const PI float64 = 3.14
const PI = 3.1415 //可以省略类型
const (
width = 100
height = 100
)
const i, j = 1, 2 //多重赋值
const a , b , c = 1, 2, "foo"
}
const constantName [type]= value
const:定义常量关键字
constName: 常量名称
type: 常量类型
value:常量值
//例子
package main
func main() {
const PI float64 = 3.14
const PI = 3.1415 //可以省略类型
const (
width = 100
height = 100
)
const i, j = 1, 2 //多重赋值
const a , b , c = 1, 2, "foo"
}
const
同时声明多个常量时,如果省略了值则表示和上面一行的值相同
go
package main
import "fmt"
func main() {
const (
a1 = 100
a2
a3
)
fmt.Printf("a1: %v\n",a1)
fmt.Printf("a2: %v\n",a2)
fmt.Printf("a3: %v\n",a3)
}
package main
import "fmt"
func main() {
const (
a1 = 100
a2
a3
)
fmt.Printf("a1: %v\n",a1)
fmt.Printf("a2: %v\n",a2)
fmt.Printf("a3: %v\n",a3)
}
iota
iota比较特殊,可以被认为是一个可被编译器修改的常量,它默认开始值是0,每调用一次加1。遇到const
关键字时被重置为0.
go
package main
import "fmt"
func main() {
const (
a1 = iota
a2 = iota
a3 = iota
)
fmt.Printf("a1: %v\n",a1)
fmt.Printf("a2: %v\n",a2)
fmt.Printf("a3: %v\n",a3)
}
package main
import "fmt"
func main() {
const (
a1 = iota
a2 = iota
a3 = iota
)
fmt.Printf("a1: %v\n",a1)
fmt.Printf("a2: %v\n",a2)
fmt.Printf("a3: %v\n",a3)
}
- 使用
_
跳过某些值
go
package main
import "fmt"
func main() {
const (
a1 = iota
_
a3 = iota
)
fmt.Printf("a1: %v\n",a1)
fmt.Printf("a2: %v\n",a2)
}
package main
import "fmt"
func main() {
const (
a1 = iota
_
a3 = iota
)
fmt.Printf("a1: %v\n",a1)
fmt.Printf("a2: %v\n",a2)
}
- 声明中间插队
go
package main
import "fmt"
func main(){
const (
a1 = iota
a2 = 100
a3 = iota
)
fmt.Printf("a1: %v\n",a1)
fmt.Printf("a2: %v\n",a2)
fmt.Printf("a3: %v\n",a3)
}
package main
import "fmt"
func main(){
const (
a1 = iota
a2 = 100
a3 = iota
)
fmt.Printf("a1: %v\n",a1)
fmt.Printf("a2: %v\n",a2)
fmt.Printf("a3: %v\n",a3)
}
https://www.bilibili.com/video/BV1zR4y1t7Wj/?p=11&vd_source=271cfb4bb43eae8c9b0543f4ae14ec31
https://www.cnblogs.com/liangyun/p/goland.html
https://gitee.com/wuyumin/GoLand/blob/master/Skill.md