当前位置:网站首页>Go language learning tutorial (10)
Go language learning tutorial (10)
2022-06-30 19:29:00 【Game programming】
One 、 Anonymous functions
* Anonymous functions are functions without names
* A normal function can be called multiple times by name , Anonymous functions have no function names , So in most cases, it is declared at the current location and called immediately ( Except for function variables )
* After the anonymous function is declared, you need to call , Close the function with curly braces
* Parameterless anonymous function
func main(){
func(){
fmt.Println(" This is an anonymous function ")
}()// Parentheses indicate calls
}
* Anonymous functions with parameters
func main() {
func(s string) {
fmt.Println(s, " This is an anonymous function ")
}(" Pass parameters ") // Pass parameters when calling
}
* Anonymous functions with parameters and return values
func main() {
r := func(s string) int {
fmt.Println(s, " This is an anonymous function ")
return 110
}(" Pass parameters ") // Pass parameters when calling
fmt.Println(r)
}
Two 、 The function variables
* stay Go Function is also a type in language , How many forms does a function have , There are many ways to write function variables
var a func() // No parameter, no return value
var b func(int) // There is one int Type parameter
var c func(int) string // There is one int Type parameters and string Type return value
fmt.Println(a, b, c) // Output :<nil> <nil> <nil>
* After defining the function variables , Anonymous functions can be used for assignment . You can also use a defined function for assignment
* After the definition of function variables, the syntax is the same as that of ordinary function calls , The variable name is the function name declared by the ordinary function
* Function type variables are in addition to slice、map、channel、interface The fifth reference type
3、 ... and 、 Standard project structure
* In actual development, there cannot be only one main package , It is impossible to have only one .go file .
* The number of packages and files varies with the size of the project
* Go The largest organizational unit in the language is the project , The project contains packages , A package can have multiple files
* A package is a folder on the physical level . Multiple files in the same folder package It has to be the same , commonly package Same as the folder name
* stay Goland Create standards in Go project
-- Project name
--src // All source code is stored in this file
-- Folder // The folder is usually the package name
--xx.go // The source code file
--mm.go // The source code file
-- Folder
--xx.go
--main.go // The file where the main function is located , Any name , Be careful main.go In the project root directory
--bin Store gadgets
--pkg Content generated after system compilation // Automatic generation , No need to create
Four 、 Package Overview
* package (package) yes Go Organizational units in languages . A package is a logical grouping . The physical grouping is different folders , Folders and packages usually correspond to each other
* Put multiple files in the same folder , These files are in the same package .
* Although source files are allowed package It is different from the folder name, but the file will be changed after final compilation package Compile to folder name . So to prevent errors, it's best to put the package Set the same as the folder name
* One Go Language projects must have main package , Any number of packages with other custom names , According to your own needs .
* Go When looking for a package, the language will start from GOPATH/src Find the package in the path , If it doesn't exist, go GOROOT/src(Go Language standard library source folder ) Look under
* Resources under different packages can access each other , After importing other packages , You can access the capitalized contents under the package
* Global resources in different files under the same package can be accessed at will
5、 ... and 、 Custom package
* Create a new project under the project src Folder , stay src New in folder demo file
* stay demo New in the file demo1.go and demo2.go file
* demo1.go The source code of the document is as follows
package demo// Package for demo
import "fmt"
func demo1(){
fmt.Println(" perform demo1")
}
* demo2.go The source code of the document is as follows
package demo// Package for demo
import "fmt"
func Demo2() {// Function names can only be accessed by other packages if they are capitalized
fmt.Println(" perform demo2")
demo1()// The content under the same package can be accessed at will
}
6、 ... and 、 Variable scope
* The variable declaration location determines the accessible range of the variable ( Where can I call variables )
* Go The valid ranges of variables in the language are as follows
* Function level : Variables are declared inside the function , Can only be accessed inside a function , Call variables local variables
* package Package level , It can be accessed under the current package . Call variables global variables . Variables are declared outside the function
* Application level , It can be accessed in any package under the whole application . Control through the case of the initial letter
7、 ... and 、 local variable
* The local variable must be inside the function
* In which {} Internal statement , Only in which {} Internal visits
8、 ... and 、 Global variables
* Global variables are declared outside the function , The entire package can be accessed
* If the global variable is capitalized , Cross package can also access .
* When declaring a global variable, the specification is
var (
Variable name
Variable name = value
)
Nine 、 Closure Overview
* Closure is not Go The unique concept of language , There are closures in many programming languages
* Closure is a solution to the problem that local variables cannot be accessed externally
* Is an application that treats a function as a return value
* The general idea is : Define local variables inside functions , Treat another function as a return value , Local variables are equivalent to global variables for return value functions , Therefore, the value of the local variable of the return value function is changed after multiple calls
Ten 、 Value passing and reference passing
* When discussing value passing and reference passing , In fact, when the value type variable and the reference type variable are used as function parameters , Whether the modification of formal parameters will affect the actual parameters
* stay Go Five reference type variables in the language , Others are value types
* slice
* map
* channel
* interface
* func()
* When reference type is used as parameter , Called a shallow copy , Formal parameters change , The real parameters follow the change . Because the address is passed , Both formal and actual parameters point to the same block of address
* When a value type is used as a parameter , Called deep copy , Formal parameters change , The actual parameters remain unchanged , Because what is passed is a copy of the value , Formal parameters will open up a new space , Different from argument point
* If you want the value type data to follow the change of the argument when modifying the formal parameter , You can set the parameter to the pointer type
11、 ... and 、 Structure
* Structural interpretation : Combine one or more variables together , Form a new type . This type is the structure
* Go The structure of language and C++ The structure is a bit similar , and Java or C# The essence of middle class is structure
* A structure is a value type
* Structure definition syntax
* It can be seen from the grammar that ,Go The inventor of the language clearly believes that a structure is a custom type
type Structure name struct{
name type // Member or attribute
}
* Defining structure
* Structs can be defined inside or outside a function ( Just like ordinary variables ), The definition location affects the access scope of the structure
* If the structure is defined outside the function , Whether the initial letter of the structure name is capitalized affects whether the structure can be accessed across packages
* If the structure can be accessed across packages , Whether the initial letter of the attribute is capitalized affects whether the attribute is accessed across packages
type People struct {
Name string
Age int
}
* Declare struct variables
* Because the structure is a value type , So the memory space will be opened up after the declaration
* All members are the initial values corresponding to the type
* You can assign values to multiple attributes of a structure directly
var peo People
// Assign values according to the order of attributes in the structure , Attribute names can be omitted
peo = People{"smallming", 17}
fmt.Println(peo)
// Specify which attributes to assign values to . You can assign values to both , You can also assign values to only part of them
peo = People{Age: 18, Name: " Jiamingge "}
fmt.Println(peo)
* You can also get the attribute through the structure variable name to assign or view
var peo People
peo.Name="smallming"
peo.Age=17
* Double class (==) Determine whether the contents in the structure are equal
author : Xiao CHENGONG
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- MQ的选择(2022.5.9-5.15)
- The cloud native landing practice of using rainbow for Tuowei information
- Development: how to install offline MySQL in Linux system?
- Evolution of screen display technology
- Memory Limit Exceeded
- 20220607 fell below the recommended retail price, and the GPU market is moving towards oversupply
- Neon optimization 2: arm optimization high frequency Instruction Summary
- How to seamlessly transition from traditional microservice framework to service grid ASM
- mysql修改数据类型_MySQL修改字段类型[通俗易懂]
- 拓維信息使用 Rainbond 的雲原生落地實踐
猜你喜欢

【UML】UML类图

Cloud Native Landing Practice Using rainbond for extension dimension information

20220528【聊聊假芯片】贪便宜往往吃大亏,盘点下那些假的内存卡和固态硬盘

熵-条件熵-联合熵-互信息-交叉熵

ArcGIS无插件加载(无偏移)天地图

Brief introduction of Feature Engineering in machine learning

Cobbler轻松上手

Task01:初识数据库与SQL(笔记1)

Electron 入门

Influence and requirements of different manufacturing processes on the pad on PCB
随机推荐
sql是否走索引
Word -- a solution for word to encounter errors when trying to open a file
Influence and requirements of different manufacturing processes on the pad on PCB
Task01:初识数据库与SQL(笔记1)
Kalman滤波器--从高斯融合推导
com. alibaba. fastjson. Jsonobject tojsonstring eliminate circular reference
Build graphql service based on Actix, async graphql, rbatis, pgsql/mysql (4) - change service
一文详解|Go 分布式链路追踪实现原理
MySQL download and installation tutorial
Opencv data type code table dtype
Some interesting modules
阿里天池SQL训练营学习笔记5
Electron 入门
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
Unity技术手册-初探性能优化
DTD modeling
com.alibaba.fastjson.JSONObject # toJSONString 消除循环引用
mysql下载和安装详细教程
在广州的朋友,有机会可以参加下
Unlimited cloud "vision" innovation | the 2022 Alibaba cloud live summit was officially launched