当前位置:网站首页>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+ a5. 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) // 18B8. 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边栏推荐
- 简述keepalived工作原理
- Several ways of JS jump link
- "Baidu Cup" CTF competition 2017 February, web:include
- 【目标检测】YOLOv5跑通VOC2007数据集
- [today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
- Cocoscreator resource encryption and decryption
- Ctfshow, information collection: web7
- Niuke real problem programming - Day17
- 【數據挖掘】視覺模式挖掘:Hog特征+餘弦相似度/k-means聚類
- 众昂矿业:萤石继续引领新能源市场增长
猜你喜欢
随机推荐
什么是pv和uv? pv、uv
MongoDB数据库基础知识整理
Change win10 Screensaver
Wechat applet 01
Spatiotemporal deformable convolution for compressed video quality enhancement (STDF)
Classification of regression tests
Several ways of JS jump link
Win10 or win11 taskbar, automatically hidden and transparent
Stream learning notes
Unity's ASE realizes cartoon flame
【OBS】RTMPSockBuf_ Fill, remote host closed connection.
【搞船日记】【Shapr3D的STL格式转Gcode】
CPU与chiplet技术杂谈
【服务器数据恢复】戴尔某型号服务器raid故障的数据恢复案例
[make a boat diary] [shapr3d STL format to gcode]
How does the database perform dynamic custom sorting?
连接ftp服务器教程
Bye, Dachang! I'm going to the factory today
Lidar Knowledge Drop
【目标检测】YOLOv5跑通VOC2007数据集








