当前位置:网站首页>Go variables and constants
Go variables and constants
2022-07-02 03:54:00 【UPythonFish】
List of articles
go Variables and constants
What is a variable
Variable specifies a storage location (Memory Location) The name of , The storage unit stores certain types of values . stay Go in , There are multiple syntax for declaring variables .
Variable defined 4 Ways of planting
func main() {
// 1. Complete definition var Variable name Variable type = A variable's value
var name string = "yxh"
var age int = 20
fmt.Println(name,age)
// 2. Type derivation var Variable name = A variable's value
var name = "yxh"
var age = 20
fmt.Println(name,age)
// 3. Make a brief statement Variable name := A variable's value
name := "yxh"
age := 20
fmt.Println(name,age)
// 4. Define multiple variables at the same time
// 4.1 Use one line definition
var name, gender string
var name, age, gender = "yxh", 20, " male "
name, age, gender := "yxh", 20, " male " // Recommended
// 4,2 understand
var (
name = "yxh"
age = 20
gender = " male "
)
var (
name string
age int
gender string
)
fmt.Println(name,age,gender)
}
Variable definition considerations :
1. After variable declaration type , It can only be modified with values of the same type
2. Short declarations can only be used inside functions , Cannot be used externally
3. Variables must be defined first , In the use of , And can only be defined once , You can only define without assignment
var age =19 // Defines a variable age
name,age :="lqz",20 // Defined variables name, Can write age, Said the use of , Modified value , It's not defined , in addition to , Nothing else
4. Variables declare that you must use , Otherwise, an error will be reported
5. Define only without assignment , Only fully defined schemes can be used
The type of variable
// Basic data type
- Numbers The following numbers represent the number of a number bit, And range size
- integer
-int // Sub platform , stay 32 Bit machine , Express int32, stay 64 Bit machine means int64
-int8 // Occupy 8 individual bit, The numerical range is ±2 Of 7 The power minus 1 -128 —— 127
-int16 // Plus or minus 2 Of 15 Power -1
-int32 // Plus or minus 2 Of 31 Power -1
-int64 // Plus or minus 2 Of 63 Power -1
- Positive integer : Unsigned integer
-uint // stay 32 Bit machine is uint32, stay 64 Bit machine is uint64
-uint8 // Occupy 8 individual bit, The numerical range is 2 Of 8 Power -1 0 —— 256
-uint16 // 2 Of 16 Power -1
-uint32 // 2 Of 32 Power -1
-uint64 // 2 Of 63 Power -1
- floating-point
-float32 // Accurate to the decimal point 6 position
-float64 // Accurate to the decimal point 15 by
- The plural ( Not used in normal development )
-complex128
-complex64
- character string
-string
- Double quotes " " or The quotation marks ` ` The parcel Backquotes indicate multiline strings , Support line break
Single quotation marks don't work : Single quotation marks indicate numbers , And only one character can be placed in single quotation marks
fmt.Println('a') // 97 a Character Unciode code
fmt.Println(' you ') // 20320 Your character Unicode code
- Boolean
-bool
-true false ( Are all lowercase )
-byte:uint8 Another name for
-rune:int32 Another name for
// Composite data type
- Array
- section
-map
// Advanced data types
- Custom type
- Function type
- Pointer types
// There are two ways to view data types
fmt.Println(reflect.TypeOf(nameOfYxh))
fmt.Printf("%T", nameOfYxh) // This can only be fmt.Printf() Use in
Type conversion
Go It has a very strict strong typology .Go There is no automatic type promotion or type conversion . You need to perform display conversion to perform corresponding operations , Columns such as
package main
import (
"fmt"
)
func main() {
i := 55 // int
j := 67.8 // float64
sum := i + j // Don't allow int + float64
sum := i + int(j) // allow
j = "yxh" // Don't allow , Report errors
fmt.Println(sum)
}
Add : stay python in int , float Wait is not the basic data type , But objects , And variable names point to memory addresses , It's not worth it , therefore python The variable name in is simple , And type conversion is also very convenient
Constant and iota
Constant , A constant quantity , stay go Once the assignment is defined in , Can't change anymore , And in the python Usually, variables in all uppercase are set as constants
The definition and use of constants
package main
import "fmt"
func main() {
// 1 The definition and use of constants
// Constant definition format const keyword Constant names type = value perhaps const keyword Constant names = value
const name string= "yxh"
// No modification allowed
name="lxx" // Report errors
fmt.Printf(name)
// Define multiple constants at the same time
const name ,age= "yxh",20
const (
name ="yxh"
age=20
)
// 2 iota: Self increasing constant
const (
a =1
b // 1 If the assignment is not written , Follow the assignment above amount to b=1
c // 1
d // 1
)
fmt.Println(a,b,c,d) // 1 1 1 1
const (
a = 1
b // 1 If the assignment is not written , Follow the assignment above amount to b=1
c = 99
d // d = 99
)
fmt.Println(a,b,c,d) // 1 1 99 99
const (
a =iota // Equate to a=0
b =iota // Self increasing
c =iota // Self increasing
d =iota // Self increasing
)
fmt.Println(a,b,c,d) // 0 1 2 3
const (
a =iota
b =100
c
d
e =iota // Every line will increase by itself
)
fmt.Println(a,b,c,d,e) // 0 100 100 100 4
const (
e, f, g = iota, iota, iota // As long as you don't change lines , It will not increase itself
h = iota
r = iota
)
fmt.Println(e, f, g, h, r) // 0 0 0 1 2
const (
a = iota + 1
b
c
)
fmt.Println(a, b, c) // 1 2 3
const (
a = 4 // Explicitly specify the value
b = 5 // Explicitly specify the value
c = iota // c = 2, Because of the iota Located at 3 individual ConstSpec,2=3-1
d = iota // d = 3, because iota It's increasing 1, Equivalent to d = iota
)
fmt.Println(a, b, c, d) // 4 5 2 3
const (
a =0 // a = 0
d=1
_ // Skip this ConstSpc, however iota Will add 1
b, c = iota, iota * 2 // iota=2
)
fmt.Println(a,b,c,d) // 0 3 6 1
// Define multiple constants at the same time , If the next line only writes the constant name , Follow the assignment method of the previous line
// iota Every new line , It's going to increase 1, Whether used or not
边栏推荐
- NLog使用
- Hands on deep learning (II) -- multi layer perceptron
- [untitled]
- Basic syntax of unity script (8) - collaborative program and destruction method
- In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
- Object oriented thinking
- Lost a few hairs, and finally learned - graph traversal -dfs and BFS
- [live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
- WPViewPDF Delphi 和 .NET 的 PDF 查看组件
- 蓝桥杯单片机省赛第五届
猜你喜欢
随机推荐
Basic operations of MySQL database (based on tables)
5G時代全面到來,淺談移動通信的前世今生
Introduction to Robotics II. Forward kinematics, MDH method
[punch in] flip the string (simple)
蓝桥杯单片机省赛第十一届
Account management of MySQL
VS2010 plug-in nuget
MySQL index, transaction and storage engine
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
Oracle common SQL
BiShe cinema ticket purchasing system based on SSM
【DesignMode】建造者模式(Builder model)
Qt插件之Qt Designer插件实现
一天上手Aurora 8B/10B IP核(5)----从Framing接口的官方例程学起
蓝桥杯单片机第四届省赛
[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真
ImageAI安装
Demonstration description of integrated base scheme
Jetpack's livedata extension mediatorlivedata









