当前位置:网站首页>Variables and constants of go language foundation
Variables and constants of go language foundation
2022-07-02 01:03:00 【Game programming】
Go Basic variables and constants of language
go Basic format of language
package main //main After the package representation, an executable file is generated // Import statement import "fmt"// The outer edge of a function can only be declared // Program entry func main() { fmt.Println("Hello world!")}Variable
package mainimport "fmt"// Variable declarations // var name string// var age int// var isOk boolvar student_name stringvar studentName string var StudentName string // Batch statement var ( // The default value is name string // "" age int // 0 isOk bool // false)// go Local variables declared in functions must use , But global variables do not have to be used \// Variables with the same name cannot be declared repeatedly in the same scope func main() { // var day int name = "lixiang" age = 18 isOk = true // Declare variables and assign values at the same time var s1 string = "luozongwei" fmt.Println(s1) // Type derivation var s2 = 1232 fmt.Println(s2) // Short variable declaration , Can only be used in functions s3 := " Hey, uh huh " fmt.Println(s3)}Anonymous variables
When using multiple assignments , If you want to ignore a value , have access to Anonymous variables (anonymous variable) . Anonymous variables are underlined _ Express , for example :
package mainimport ( "fmt")func foo() (int, string){ // In the second bracket is the return type of the function return 10, "Luozongwei"}func main() { x, _ := foo() _, y := foo() fmt.Println("x=", x) fmt.Println("y=", y)} Anonymous variables do not take up the namespace , Memory will not be allocated , So there is no duplicate declaration between anonymous variables . ( stay Lua And so on , Anonymous variables are also called dummy variables .)
matters needing attention :
- Every statement outside the function must start with a keyword (var 、const、func etc. )
:=Can't be used outside of a function ._It is often used for space occupying , Indicates the ignored value .
Constant
Relative to variables , A constant is a constant value , It is often used to define the values that will not change during the running of the program . The declaration of constants is very similar to the declaration of variables , Just put var Instead of const , Constants must be defined with values .
const pi = 3.1415const e = 2.7182 The statement pi and e After these two constants , Their values can no longer be changed during the entire program run .
Multiple constants can also be declared together :
const ( pi = 3.1415 e = 2.7182)const When multiple constants are declared at the same time , If the value is omitted, it means that it is the same as the value in the previous line . for example :
const ( n1 = 100 n2 n3) In the above example , Constant n1 、 n2 、 n3 The values are all 100.
iota
iota yes go Constant counter of language , Can only be used in constant expressions .iota stay const The keyword will be reset to 0.const Every new line of constant declaration in will cause iota Count once (iota Can be understood as const Line index in statement block ). Use iota Can simplify the definition of , Useful when defining enumerations .
for instance :
const ( n1 = iota //0 n2 //1 n3 //2 n4 //3 ) Several common iota Example :
Use _ Skip some values
const ( n1 = iota //0 n2 //1 _ n4 //3 )iota Make a statement to jump in the middle
const ( n1 = iota //0 n2 = 100 //100 n3 = iota //2 n4 //3 ) const n5 = iota //0 Define the order of magnitude ( there << Indicates move left operation , 1<<10 It means that you will 1 The binary representation of is shifted to the left 10 position , That is, by 1 Turned into 10000000000 , That's decimal 1024. Empathy 2<<2 It means that you will 2 The binary representation of is shifted to the left 2 position , That is, by 10 Turned into 1000 , That's decimal 8.)
const ( _ = iota KB = 1 << (10 * iota) MB = 1 << (10 * iota) GB = 1 << (10 * iota) TB = 1 << (10 * iota) PB = 1 << (10 * iota) ) Multiple iota It's defined on one line
const ( a, b = iota + 1, iota + 2 //1,2 c, d //2,3 e, f //3,4 ) towards b Standing on the achievements of the old boy teacher Li Wenzhou !!!
author :asddsa1233122132
this paper [ Go Basic variables and constants of language ] Included in Game programming ️ - Go, A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome browser .
边栏推荐
- With the acquisition of Xilinx, AMD is more than "walking on two legs" | Jiazi found
- Collection: comprehensive summary of storage knowledge
- Leetcode skimming: stack and queue 03 (valid parentheses)
- Global and Chinese market of aircraft MRO software 2022-2028: Research Report on technology, participants, trends, market size and share
- Promise和模块块化编程
- Global and Chinese market of picture archiving and communication system (PACS) 2022-2028: Research Report on technology, participants, trends, market size and share
- [eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)
- [opencv450] hog+svm and hog+cascade for pedestrian detection
- Global and Chinese markets for distributed generation and energy storage in telecommunications networks 2022-2028: Research Report on technology, participants, trends, market size and share
- Leetcode skimming: stack and queue 06 (top k high-frequency elements)
猜你喜欢

How to type spaces in latex

Slf4j print abnormal stack information

Kuberntes cloud native combat high availability deployment architecture

Schrodinger's Japanese learning applet source code

Leetcode skimming: binary tree 01 (preorder traversal of binary tree)

【八大排序③】快速排序(动图演绎Hoare法、挖坑法、前后指针法)

The new version of graphic network PDF will be released soon

【底部弹出-选择器】uniapp Picker组件——底部弹起的滚动选择器

What skills does an excellent software tester need to master?

Geek DIY open source solution sharing - digital amplitude frequency equalization power amplifier design (practical embedded electronic design works, comprehensive practice of software and hardware)
随机推荐
gradle
Geek DIY open source solution sharing - digital amplitude frequency equalization power amplifier design (practical embedded electronic design works, comprehensive practice of software and hardware)
Leetcode skimming: binary tree 02 (middle order traversal of binary tree)
Global and Chinese market of safety detection systems 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for supply chain strategy and operation consulting 2022-2028: Research Report on technology, participants, trends, market size and share
Mathematics - feelings -20220215
Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
Cat Party (Easy Edition)
Otaku wallpaper Daquan wechat applet source code - with dynamic wallpaper to support a variety of traffic owners
If the browser is accidentally closed, how does react cache the forms filled out by users?
什么是商业养老保险?商业养老保险安全靠谱吗?
Picture puzzle wechat applet source code_ Support multi template production and traffic master
[eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)
Global and Chinese market of picture archiving and communication system (PACS) 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for food allergens and intolerance tests 2022-2028: Research Report on technology, participants, trends, market size and share
Advanced skills of testers: a guide to the application of unit test reports
Synthetic watermelon game wechat applet source code / wechat game applet source code
[bottom pop-up selector] uniapp picker component - scroll selector popped up at the bottom
@Valid参数校验不生效
970 golang realizes the communication between multithreaded server and client