当前位置:网站首页>Go learning notes - constants
Go learning notes - constants
2022-07-04 07:23:00 【InfoQ】
background —— Everything must have a background
Before we get to the point
Notes
- It is created at the beginning of compilation , The value remains unchanged throughout the life cycle , The advantage is concurrent scenarios , Don't consider the synchronization of constants , This is basically the same as the constant definition of other languages
- Native support , Type safety
- go Language provides many innovations about constants , It is convenient for applications in different scenarios , Here I personally like to use constants to implement the enumeration feature .
- Untyped constant, seeing the name of a thing one thinks of its function , That is, constants that do not seem to define numeric types ( Here's a little more ,go The constant definition of language only supports strings , Numeric and Boolean types ), But in fact, the value type will be automatically assigned according to the initial value .
package main
import "fmt"
const n = 1 // Define a typeless constant , According to the initial value, we will give n Defined as int type
func main() {
var a int = 5
fmt.printIn(a + n) // Output 6
}
- Implicit conversion
package main
import "fmt"
type myInt int // Custom numeric type , The bottom layer is int
const n = 2 // Define a typeless constant , According to the initial value, we will give n Defined as int type
func main() {
var a myInt = 5
fmt.printIn(a + n) // Output 7
}
- Implement enumeration
- go Language primitives have no enumeration types (enum)
- Go Of const Syntax provides “ Implicitly repeats the previous non empty expression ” The mechanism of
- Go On the basis of the above features, it also provides “ Artifact ”:iota,
package main
import "fmt"
const (
Apple, Banana = iota, iota + 10 // 0, 10 (iota = 0)
Strawberry, Grape // 1, 11 (iota = 1)
Pear, Watermelon // 2, 12 (iota = 2)
)
const (
_ = iota // 0, White space characters
Pin1
Pin2
Pin3
_ // Use white space characters , skip Pin4, But then Pin5 The value of is still on time 5, Follow the standard library code $GOROOT/src/syscall/net_js.go
Pin5 // 5
)
//iota Better application scenarios , such as , The first enumeration type is only X,M,L, And if you want to add a new size later , Just add a new type to the corresponding enumeration
// In the previous way, all constants need to be manually modified
const (
_ = iota
S
M
//XM
L
)
func main() {
fmt.Println(Apple)
fmt.Println(Banana)
fmt.Println(Pear)
fmt.Println(Watermelon)
fmt.Println(Pin1)
fmt.Println(Pin5)
fmt.Println(S)
fmt.Println(L) // You can let go XM After the comments , see L It's going to change
}
边栏推荐
- Docker install MySQL
- Two years ago, the United States was reluctant to sell chips, but now there are mountains of chips begging China for help
- 2022 - 021arts: début du deuxième semestre
- [real case] how to deal with the failure of message consumption?
- 博客停更声明
- Cochez une colonne d'affichage dans une colonne de tableau connue
- Novel website program source code that can be automatically collected
- 《剑指Offer》第2版——力扣刷题
- 在已经知道表格列勾选一个显示一列
- Crawler (III) crawling house prices in Tianjin
猜你喜欢
Computer connects raspberry pie remotely through putty
Rhcsa the next day
[kubernetes series] kubesphere is installed on kubernetes
Vulhub vulnerability recurrence 77_ zabbix
Responsive mobile web test questions
Review of enterprise security incidents: how can enterprises do a good job in preventing source code leakage?
Zephyr 学习笔记1,threads
The final week, I split
Deep profile data leakage prevention scheme
Introduction to rce in attack and defense world
随机推荐
【Kubernetes系列】Kubernetes 上安装 KubeSphere
What is the use of cloud redis? How to use cloud redis?
Literature collation and thesis reading methods
Cell reports: Wei Fuwen group of the Institute of zoology, Chinese Academy of Sciences analyzes the function of seasonal changes in the intestinal flora of giant pandas
The final week, I split
Campus network problems
Zabbix agent主动模式的实现
Experience installing VMware esxi 6.7 under VMware Workstation 16
How to input single quotation marks and double quotation marks in latex?
Boosting the Performance of Video Compression Artifact Reduction with Reference Frame Proposals and
How can the old version of commonly used SQL be migrated to the new version?
2022-021ARTS:下半年開始
Cochez une colonne d'affichage dans une colonne de tableau connue
Boast about Devops
Vulhub vulnerability recurrence 76_ XXL-JOB
Centos8 install mysql 7 unable to start up
Deep profile data leakage prevention scheme
uniapp小程序分包
Unity 从Inspector界面打开资源管理器选择并记录文件路径
提升复杂场景三维重建精度 | 基于PaddleSeg分割无人机遥感影像