This is the 13th day of my participation in the August More Text Challenge. For details, see:August is more challenging

The adorable Golang mascot was created by Renee French, wife of Rob Pike, one of Golang’s designers

Developed by Google in 2007 and open sourced in 2009, Golang is now more than a decade old. Since the 1.0 stable release in 2012, Golang has attracted a large number of developers and developed a large active community around the world.

Golang has a native design advantage in multi-core concurrency, taking full advantage of modern hardware performance and development efficiency. It is designed to take advantage of the speed of dynamic languages such as Python and provide the performance and security of compiled languages such as C/C++.

Golang has strong language expression capabilities, supports static type safety, and can quickly compile large projects; It also allows developers to access the underlying operating system and exploit the CPU resources of the computer. It also provides powerful network programming and concurrent programming support.

Golang has the following features:

  • Support concurrency from the bottom, without third-party library support, low requirements for developers’ programming skills and development experience;
  • Supports automatic garbage collection to avoid memory leaks
  • Supports multiple return values for functions
  • Support for anonymous functions and closures
  • Support the reflection
  • Better performance
  • Provides strong standard library support
  • A fast, statically typed editing language that also provides dynamic language features

Golang’s simple, efficient and concurrent features have attracted many developers to join the development family of Golang. At present, a large number of large open source projects have emerged through the native development of Golang, and play an important role in the software industry, including Docker, Kubernetes, etc.

Source code and program

  • In order to.goFor file suffixes, allgoFiles are stored in packages, and a package consists of one or more GO files
  • usepackageThe keyword declares which package the current GO file belongs to
  • useimportKeyword to import external packages
  • Main is a special package that defines a single executable program; inmainIn the package,mainPhi is a special function

annotation

Comments are a useful feature. As the program grows larger and more complex, it is necessary to add comments to explain the module, so that the intent of the current code block or module can be quickly known even when the project is opened again after a long time

Single-line comments

Use a double slash to indicate a single line comment in a program: //; Such as:

func main(a) {
  // conversion()
  // join()
  // replace()
  // number()
  // error()
}
Copy the code

If you add a single-line comment in front of your code, the parser will not parse the commented code!

Multiline comment

Use the /* */ symbol in a program to represent multi-line comments; Such as:

/* func error() {} */
Copy the code

In the above code, the error() function is ignored and will not be executed

variable

A variable is a name for a block of memory. A program can claim a block of memory by defining a variable and use the memory by referring to a variable

25 keywords in the Go language

/ / package
import,package

// Declare related
var,const,type,struct,interface,func,chan,map,go

// Loop related
for,range

// The condition is related
if,else,switch,select,case

// Interrupt or return
return,goto,fallthrough,break,default,continue

// Delay execution
defer
Copy the code

Built-in pre-declared constants, types, and functions

/ / constant
true,false,iota,nil

/ / integer
int,int8,int16,int32,int64

// Unsigned integer
uint,uint8,uint16,uint32,uint64,uintptr

/ / floating point
float32,float64,complex64,complex128

/ / other
bool,byte,rune,stringAnd the error/ / function
make,len,cap,new,append,copy,close,delete,complex,real,imag,panic,recover
Copy the code

The scope of a variable

  • Variables that start with an uppercase letter are exported and visible and accessible outside the package. If it starts with a lowercase letter, it can only be used within the current package
  • Use the var keyword to create a variable of some type and set the initial value; Type and initial value can be omitted, but not both; When the initial value is omitted, the value of the variable defaults to zero for the current type; Such as:intThe type is 0,booleanThe type is false, the string is “”, and the zero value of the interface and application type isnilThe zero value of, array, and structure is the zero value of all members.

Short variable

  • use: =Declare variables and assign values; Such as:
name := "Forest"
Copy the code
  • Variables declared this way can only be used inside functions

The statement period

  • The declaration period is the start and end time referenced by other parts of the program when the program is running

  • The declaration period of a variable refers to the period during which the variable exists during the execution of the program

    • Global variables: Package-level variables are declared in the execution time of the entire program
    • Local variables: The declaration period of local variables is dynamic. The parameters and return values of functions are local variables. The function is created when it is called and destroyed when it is finished
  • When a variable is created in uncertain memory, it is allocated to the heap; Such as slice, map, channel, and so on. If the memory allocated exceeds the size of the stack, it is allocated to the heap

    • Heap: A dynamically scalable segment of memory that is allocated dynamically during the execution of a process
    • Stack: Used to hold local variables temporarily created by a program, that is, variables defined inside a function or arguments passed in a call to a function

scope

  • Scope is the location and valid scope of the declaration in the program

    • When compiling, the program will start from the current scope to the outer layer of the search, if not found an error; If both the inner and outer layers have this variable, the inner one is used first
  • Access permissions

    • Package level private
    • Module-level private
    • Public class