当前位置:网站首页>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 .
边栏推荐
- 在广州的朋友,有机会可以参加下
- How to improve the three passive situations in data analysis
- slice
- BeanUtils.copyProperties() 对比 mapstruct
- 20200525 Biotechnology - Sichuan Normal University self taught Biotechnology (undergraduate) examination plan txt
- France a+ France VOC label highest environmental protection level
- Sqlserver SQL Server Management Studio and transact SQL create accounts and create read-only users to access the specified database
- Redis beginner to master 01
- How to configure webrtc video stream format playback in the new version of easygbs?
- 20220607 fell below the recommended retail price, and the GPU market is moving towards oversupply
猜你喜欢
随机推荐
【DesignMode】单例模式(singleton pattern)
Memory Limit Exceeded
Mipi protocol in RFFE
企业选型作业上常犯的一个错误
LinkedList的简单用法(2022.6.13-6.19)
全技术栈、全场景、全角色云原生系列培训重磅首发,助力企业打造硬核云原生技术团队
不同制造工艺对PCB上的焊盘的影响和要求
Pyth-Solana链上联通现实的桥梁
Large file transfer software based on UDP protocol
Lenovo Yoga 27 2022, full upgrade of super configuration
设计电商秒杀系统
虚拟主机什么时候适合更换成云主机?
开发那些事儿:Linux系统中如何安装离线版本MySQL?
Task01:初识数据库与SQL(笔记1)
JVM常见问题
Redis beginner to master 01
MySQL recursion
MySQL function to get the full path
MQ的选择(2022.5.9-5.15)
新版EasyGBS如何配置WebRTC视频流格式播放?



![[JetsonNano] [教程] [入门系列] [一] 如何开启VNC共享](/img/f5/3f0f69739caa22809f40cf4b7483fe.png)





