当前位置:网站首页>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 .
边栏推荐
- ArcGIS no plug-in load (no offset) day map
- Cloud Native Landing Practice Using rainbond for extension dimension information
- Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
- How to use the low code platform of the Internet of things for service management?
- Word——Word在试图打开文件时遇到错误的一种解决办法
- Go语言学习教程(十)
- 一文详解|Go 分布式链路追踪实现原理
- 【DesignMode】单例模式(singleton pattern)
- 德国AgBB VoC有害物质测试
- 20200525-生物技术-四川师范大学自考生物技术(本科)考试计划.txt
猜你喜欢

ANSI/UL 94 5-V级垂直燃烧试验

商业智能BI与业务管理决策思维之四:业务成本分析

浏览器窗口切换激活事件 visibilitychange

Connect to lab server

Unlimited cloud "vision" innovation | the 2022 Alibaba cloud live summit was officially launched

Evolution of screen display technology

Swin-Transformer(2021-08)

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

假期安全不放假,VR交通让孩子安全出行|广州华锐视点

力扣------统计包含给定前缀的字符串
随机推荐
德国AgBB VoC有害物质测试
Small notes - integer improvement (C language)
配置服务器环境
Entropy - conditional entropy - joint entropy - mutual information - cross entropy
Evolution of screen display technology
MySQL 索引测试
Redis入门到精通01
4个技巧告诉你,如何使用SMS促进业务销售?
Development: how to install offline MySQL in Linux system?
Opencv data type code table dtype
ROS advertisement data publishing tips - latch configuration
Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 1)
A common mistake in enterprise model selection
Swin-Transformer(2021-08)
sql连续登录问题
Kalman滤波器--从高斯融合推导
Influence and requirements of different manufacturing processes on the pad on PCB
Unity技术手册-初探性能优化
码蹄集 - MT3111· 赋值
Business Intelligence BI and business management decision-making thinking 4: business cost analysis