当前位置:网站首页>Variable declaration of go language
Variable declaration of go language
2022-06-23 15:34:00 【Mengfei】
List of articles
1 General grammar
var Variable name Variable type Multiple variables of the same type can be declared as a statement :
var Variable name 1, Variable name 2, Variable name 3 Variable type Variable declarations can be Package level Or Function level Of , Such as :
package main
import "fmt"
var c, python, java bool // Package level variable declaration
func main() {
var i int // Function level variable declaration
fmt.Println(i, c, python, java)
}2 Add initializer
You can add an initializer to each variable (initializer):
var Variable name 1, Variable name 2 Variable type = The initializer 1, The initializer 2Note that the number of initializers must be the same as the number of variables . With initializer , Variable types can be omitted , The type of this variable is automatically inferred from the initializer . Example :
package main
import "fmt"
var i, j int = 1, 2
func main() {
var c, python, java = true, false, "no!" // With initializer , Variable types can be omitted
fmt.Println(i, j, c, python, java)
}3 Simplify variable declarations
Within the function , When implicit type declarations are used ( There is an initializer , Omit variable type ), Use simpler statements :
Variable name := The initializer Example :
package main
import "fmt"
func main() {
var i, j int = 1, 2
k := 3
c, python, java := true, false, "no!"
fmt.Println(i, j, k, c, python, java)
}Be careful : Outside the function , Every statement must use the keyword (var, func etc. ) Start , So it can't be used := Structure .
4 Declare variables in chunks
Variables can be declared as a block , Make the structure more clear :
package main
import (
"fmt"
"math/cmplx"
)
var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1
z complex128 = cmplx.Sqrt(-5 + 12i)
)
func main() {
fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe)
fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
fmt.Printf("Type: %T Value: %v\n", z, z)
}5 Basic types
go The basic types are :
bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8
rune // alias for int32
// represents a Unicode code point
float32 float64
complex64 complex128Integer numbers are generally used int type , Unless there is a special reason to use other byte lengths or unsigned integers . When there is no initializer , Default values for various types :
- Numeric type :0
- bool type :false
- string type :""
6 Type conversion
When the type of assignment is different from the type of variable , Type conversion required :
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)Or it can be simplified to :
i := 42
f := float64(i)
u := uint(f)Be careful go There is no implicit type conversion , Display type conversion must be used .
7 Type inference
When we use implicit type declarations , The variable type will be based on the value on the right side of the declaration statement ( The initializer ) Make inferences .
- When the value on the right is a variable of known type , The type of the variable on the left is the same :
var i int
j := i // j Also for the int type - When the value on the right is a numeric literal constant , Then the variable on the left is int, float64, or complex128 type .
i := 42 // int
f := 3.142 // float64
g := 0.867 + 0.5i // complex128- When the value on the right is bool or string Literal measure time , The variables on the left are of corresponding types .
8 Constant type
The declaration of constants is similar to that of variables , Just will var Change the keyword to const, And can't use := Make a statement . Example :
package main
import "fmt"
const Pi = 3.14
func main() {
const World = "world"
fmt.Println("Hello", World)
fmt.Println("Happy", Pi, "Day")
const Truth = true
fmt.Println("Go rules?", Truth)
}9 Numerical constants
Numeric constants are high-precision values . When a numeric constant does not declare a type , Its type is not inferred from the value on the right like a variable , The type will be determined according to the context . Example :
package main
import "fmt"
const (
// Create a huge number by shifting a 1 bit left 100 places.
// In other words, the binary number that is 1 followed by 100 zeroes.
Big = 1 << 100
// Shift it right again 99 places, so we end up with 1<<1, or 2.
Small = Big >> 99
)
func needInt(x int) int { return x*10 + 1 }
func needFloat(x float64) float64 {
return x * 0.1
}
func main() {
fmt.Println(needInt(Small)) // 21
fmt.Println(needFloat(Small)) // 0.2
fmt.Println(needFloat(Big)) // 1.2676506002282295e+29
fmt.Println(needInt(Big)) // Report errors :constant 1267650600228229401496703205376 overflows int
}reference : A tour of gogo Reference documents Go’s Declaration Syntax
边栏推荐
- Analysis of graphical level-1 programming problem of Electronic Society: cat and mouse
- How can genetic testing help patients fight disease?
- idea查看.class文件 idea查看.class文件夹
- JS create an array (literal)
- 【云驻共创】智能供应链计划:提升供应链决策水平,帮助企业运营降本增效
- Important knowledge of golang: sync Cond mechanism
- 32. Compose 优美的触摸动画
- 【opencv450】椒盐噪声demo
- List query sorting parameter processing
- Raspberry PI installing the wiring pi
猜你喜欢

Effect evaluation of regression model under credit product quota pricing scenario

Important knowledge of golang: rwmutex read / write lock analysis

《墨者学院——SQL手工注入漏洞测试(MySQL数据库)》

The largest IPO of Hong Kong stocks this year, with a net worth of 66billion, is the "King" sitting on the mine

WebService interface publishing and calling

2021-05-08

Moher College - manual SQL injection vulnerability test (MySQL database)

Uniswap acquires genie, an NFT transaction aggregator. Will the NFT transaction market change?
Redis缓存三大异常的处理方案梳理总结

基因检测,如何帮助患者对抗疾病?
随机推荐
How can genetic testing help patients fight disease?
Explain in detail the principle and implementation of redis distributed lock
MySQL series: storage engine
MySQL advanced statement I
Sectigo(Comodo)证书的由来
Important knowledge of golang: rwmutex read / write lock analysis
After nine years at the helm, the founding CEO of Allen Institute retired with honor! He predicted that Chinese AI would lead the world
Solution to the problem that MySQL cannot be started in xampp
[cloud based co creation] how manufacturing enterprises build "barcode factories"
基因检测,如何帮助患者对抗疾病?
[普通物理] 半波损失 等厚与等倾干涉
从3开始,在业务系统中增加分页功能
MySQL高级语句二
volatile~多线程下变量不可见
Important knowledge of golang: mutex
Xampp中mysql无法启动问题的解决方法
JS create an array (literal)
VIM backup history command
SQL注入漏洞(原理篇)
2022年个人理财利率是多少?个人如何选择理财产品?