当前位置:网站首页>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
}
边栏推荐
- Campus network problems
- Two years ago, the United States was reluctant to sell chips, but now there are mountains of chips begging China for help
- Chain ide -- the infrastructure of the metauniverse
- 手写简易版flexible.js以及源码分析
- Master-slave replication principle of MySQL database
- win10微软拼音输入法输入文字时候下方不出现中文提示
- Types of references in BibTex
- tornado项目之路由装饰器
- Amd RX 7000 Series graphics card product line exposure: two generations of core and process mix and match
- Cochez une colonne d'affichage dans une colonne de tableau connue
猜你喜欢

Implementation of ZABBIX agent active mode

win10微软拼音输入法输入文字时候下方不出现中文提示

《剑指Offer》第2版——力扣刷题

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

About how idea sets up shortcut key sets

响应式——媒体查询

Status of the thread

The cloud native programming challenge ended, and Alibaba cloud launched the first white paper on application liveliness technology in the field of cloud native

Zephyr Learning note 2, Scheduling

Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction
随机推荐
The crackdown on Huawei prompted made in China to join forces to fight back, and another enterprise announced to invest 100 billion in R & D
How to buy financial products in 2022?
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
Since DMS is upgraded to a new version, my previous SQL is in the old version of DMS. In this case, how can I retrieve my previous SQL?
Introduction to rce in attack and defense world
Electronic Association C language level 1 35, bank interest
手写简易版flexible.js以及源码分析
[FPGA tutorial case 8] design and implementation of frequency divider based on Verilog
在已經知道錶格列勾選一個顯示一列
Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction
BasicVSR++: Improving Video Super-Resolutionwith Enhanced Propagation and Alignment
The final week, I split
Su Weijie, a member of Qingyuan Association and an assistant professor at the University of Pennsylvania, won the first Siam Youth Award for data science, focusing on privacy data protection, etc
"Sword finger offer" 2nd Edition - force button brush question
NLP-文献阅读总结
Zabbix agent主动模式的实现
A real penetration test
The cloud native programming challenge ended, and Alibaba cloud launched the first white paper on application liveliness technology in the field of cloud native
Campus network problems
Implementation of ZABBIX agent active mode