当前位置:网站首页>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
边栏推荐
- Object oriented thinking
- Oracle 查看被锁的表和解锁
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验2 蜂鸣器实验(学习笔记)
- Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
- MD5 of Oracle
- 蓝桥杯单片机数码管技巧
- Suggestions on settlement solution of u standard contract position explosion
- 蓝桥杯单片机省赛第十一届第一场
- 蓝桥杯单片机省赛第五届
- Lost a few hairs, and finally learned - graph traversal -dfs and BFS
猜你喜欢

手撕——排序

【DesignMode】建造者模式(Builder model)

WPViewPDF Delphi 和 .NET 的 PDF 查看组件

Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map

高性能 低功耗Cortex-A53核心板 | i.MX8M Mini

蓝桥杯单片机第四届省赛

Hands on deep learning (II) -- multi layer perceptron

How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?

The first practical project of software tester: web side (video tutorial + document + use case library)

Didi open source Delta: AI developers can easily train natural language models
随机推荐
In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
Basic operations of MySQL database (based on tables)
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
Is the product of cancer prevention medical insurance safe?
Blue Bridge Cup single chip microcomputer sixth temperature recorder
Introduction to Robotics II. Forward kinematics, MDH method
The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
【leetcode】74. Search 2D matrix
【DesignMode】原型模式(prototype pattern)
SQL: common SQL commands
WPViewPDF Delphi 和 .NET 的 PDF 查看组件
[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
[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!
Opencv learning example code 3.2.4 LUT
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真
Failed to upgrade schema, error: “file does not exist
The 9th Blue Bridge Cup single chip microcomputer provincial competition
高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
leetcode-1380. Lucky number in matrix