当前位置:网站首页>2. Basic knowledge of golang
2. Basic knowledge of golang
2022-07-07 15:16:00 【Indulge fly】
Catalog
3. Naming specification ( Suggest )
1. identifier
identifier : Just name it , The composition rules are as follows
1. Identifiers are made up of letters , Numbers and underscores
2. Cannot start with a number
3. Identifiers are case sensitive
2. keyword
1. Keywords are built-in characters , Cannot be used as an identifier
2. common 25 Two keywords and 36 Predefined identifiers
3. Don't have to remember , You'll get familiar with it slowly
3. Naming specification ( Suggest )
1.Go It's case sensitive language
2. When an identifier is referenced as an external package , The first letter should be capitalized
When the identifier is not visible by outsourcing , Initial lowercase
3. Package name :
Any one of them go Files can be referenced as packages ,
After naming the package name (package Package name ) when , Try to keep consistent with the directory , Or meaningful
4.go File naming : A lowercase letter + Underline
5. Structure naming : Hump nomenclature
6. The interface name : With "er" ending
7. Constant naming : All capitals + Underline
8. unit testing : The file is named with "_test.go" ending , The function name is "Test" start
4. Variable
1. The variable of reputation cannot be repeated , And after the declaration, you must use
2. Externally declared variables :var Variable name Variable type
3. Batch variable declaration :
var(
name string
sex string
age int
)
4. Variable initialization : use = assignment
var name = "fly"
5. Variable type derivation : During initialization , Don't declare data types ,go Internal will automatically deduce
var(
name = "fly"
age = 18
sex = "man"
)
6. Initialize multiple variables : Separated by commas , One to one assignment
var name, age, sex = "fly", 18, "man"
7. Short variable declaration : Refers to variables declared inside a function , You don't have to write var, use ":="
fun main() {
name := "zcy"
age = 18
sex = "man"
}
8. Anonymous variables : When receiving variables as parameters , Unused variables are underlined "_" Instead of
package main
import "fmt"
func getNameAge() (string, int) {
return "fly", 18
}
func main() {
name, _ := getNameAge()
fmt.Printf("name: %v\n", name)
}
9. notes :
Single-line comments : Double diagonal bar // ctrl + /
Multiline comment :/* */ alt + shif t+ a
5. Constant
1. Concept : Constants are defined in the compilation stage , Unalterable
2. Constant definition : Use keywords const
usage :const Constant names type = Constant values
3. Because when defining constants, you must assign values , So constant types can be omitted
4. Batch definition constants
const (
width = 100
height = 100
)
5. When defining constants in batch , If the following constant omits the assignment , The default is the same as the previous variable
const (
hight = 100 // 100
weidth // 100
name = "fly" // fly
age // fly
)
6. Continuous definition ( Multiple assignments ):
const width, height = 100, 100
7.iota
(1)iota Is a constant that can be modified by the compiler , The default is 0, Every time you call +1, encounter const When the reset to 0
(2) Both constants define iota, There is a gap between the two "_" Or ordinary constants ,iota The value of will still jump
const (
a = iota // 0
_ // encounter "_",iota still +1, here =1
b = iota // 2
c = 1 // 1 Encountered a common variable ,iota still +1, here =3
d = iota // 4
)
6. Data type classification
1. Boolean type
2. Numeric type ( integer , floating-point )
3. String type ( Fixed length )
4. Derived type ( Pointer types , An array type , Structured type ,Channel type , Function type , Slice type , Interface type (interface),Map type )
7. Basic supplement
1. Judge if
if Conditions {
The body of the function
} else {
The body of the function
}
2. loop for
for The initial state ; Conditions ; State change {
The body of the function
}
3. And :&&
4. Print variables
fmt.Println( Variable )
5. Format printing
fmt.Printf("%v\n", Variable )
difference :Println Bring your own line feed
5. Print symbols
Print data :%v
fmt.Printf("%v\n", "FLY") // FLY
Print data type :%T
fmt.Printf("%T\n", "fly") // string
Print number type integer size :%dB
fmt.Printf("%dB\n", 18) // 18B
8. Boolean type
true,false
effect : In conditional judgment , loop , Logical expression, etc , Used to judge true or false
Be careful :golang Boolean type in , You can't use numbers (0 Or not 0) Express true and false
package main
import "fmt"
func main() {
age = 200
// error
if age {
fmt.Println(" error ")
}
// correct
if age>1 {
fmt.Println(" correct ")
}
}
9. Numeric type
1. Include : Integer and floating point , Primitive plural , Bit operation uses complement
2. integer :int integer ,uint Unsigned integer ,uintptr Unsigned integer with fixed length to store pointer
3.int And uint stay 32 Bit on the operating system 4 Bytes , stay 64 Bit on the operating system 8 Bytes
4.go The sizes of the types in are fixed ( understand ), among int Type is the fastest kind of calculation
5. floating-point :float32 And float64( Add : Floating point numbers in computers are imprecise !)
(1) The integer zero value is 0, Floating point zero is 0.0
(2) Format and print floating point :
Full print :%f
Take two digits to print :%.2f
6. Hexadecimal conversion Binary format output
Decimal system :%d
Binary system :%b
octal :%o
Hexadecimal :%x or %X
7. The plural :
32 position :complex64
64 position :complex128
边栏推荐
- 数学建模——什么是数学建模
- "Baidu Cup" CTF competition 2017 February, web:include
- Niuke real problem programming - Day9
- [follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
- 智汀不用Home Assistant让小米智能家居接入HomeKit
- 银行需要搭建智能客服模块的中台能力,驱动全场景智能客服务升级
- 防火墙基础之服务器区的防护策略
- TypeScript 发布 4.8 beta 版本
- [server data recovery] data recovery case of raid failure of a Dell server
- CPU与chiplet技术杂谈
猜你喜欢
全日制研究生和非全日制研究生的区别!
Notes HCIA
Niuke real problem programming - Day12
"Baidu Cup" CTF competition 2017 February, web:include
#HPDC智能基座人才发展峰会随笔
Ctfshow, information collection: Web3
Wechat applet 01
Niuke real problem programming - day20
CTFshow,信息搜集:web7
Write a ten thousand word long article "CAS spin lock" to send Jay's new album to the top of the hot list
随机推荐
How does the database perform dynamic custom sorting?
CTFshow,信息搜集:web13
Used by Jetson AgX Orin canfd
Ctfshow, information collection: web8
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
Discussion on CPU and chiplet Technology
Yyds dry goods inventory # solve the real problem of famous enterprises: cross line
Oracle控制文件丢失恢复归档模式方法
[Data Mining] Visual Pattern Mining: Hog Feature + cosinus Similarity / K - means Clustering
Classification of regression tests
Jetson AGX Orin CANFD 使用
MySQL bit type resolution
银行需要搭建智能客服模块的中台能力,驱动全场景智能客服务升级
CPU与chiplet技术杂谈
[target detection] yolov5 Runtong voc2007 data set
Niuke real problem programming - Day9
2022年5月互联网医疗领域月度观察
大表delete删数据导致数据库异常解决
How bad can a programmer be? Nima, they are all talents
There is a cow, which gives birth to a heifer at the beginning of each year. Each heifer has a heifer at the beginning of each year since the fourth year. Please program how many cows are there in the