ArticleDirectory
- 1.1 Function Definition
- 1.2 member function definition
- 1.3 function nesting
- 2.1 gopath
1. Define function 1.1
Go supports anonymous functions and closures. Similar to Python, go functions can return multiple values. Go is also a statically compiled language. Let's take a look at the format of the Go function:
Func function name (parameter table...) [(return value...)] {// function body}
Note:
1. the return value is defined after the parameter table. If there is only one return value, parentheses on both sides of the return value are not required. If there is no return value, the return value can be omitted.
2. The start braces of the function body must be at the end of the row. This is mandatory, as is the case in IF and switch.
3. If the function name starts with a letter, it is externally visible (exported, public ). If it is a lowercase letter, it is private within the package level.
1.2 member function definition
Func (T * type) function name (parameter table...) [(return value...)] {// function body}
In this way, a member function can be defined for type.
1.3 function nesting
You can define anonymous functions in a function body, or even you can define anonymous functions, and then use variables to reference this anonymous function. However, you cannot nest a name function in the function body.
Func main () {func () {FMT. println ("anonymous func is OK")} () var Foo = func () {FMT. println ("using a variable to refer anonymous func is OK")} Foo () func notallow () {FMT. println ("You can not define named func ")}}
Here, the anonymous function has the semantics of value type. You can treat anonymous functions like a value (when a parameter is passed, as a function return value ). However, the name function does not enjoy this preferential treatment. Compared with JavaScript or lisp, it lacks the ability to define functions at any location. In go, anonymous functions have this capability.
2. Build a system (build) 2.1 gopath
To add the current project directory to gopath, you can use $ go env to check your environment variables, including gopath.
If you want to list all packages in the current directory, you can use
$ Go list ./...
After gopath is set, all your commands starting with go are started with the project directory specified by gopath.
For example:
$ Go test algorithm/bubblesort # The Directory is [project root] src/algorithm/bubblesort. Go test is used to find test packages in the src directory.
3. Three points (...)
For a function that defines an indefinite number of parameters, three points are required... :
Func (a, B INT, Z float64, opt... interface {})
The last variable is an indefinite parameter of any type... .
There is also a common application... Add an element to the array slice. When the added element is of the array or array slice type, append (ARR ,... T) the element needs to be broken down and passed as an indefinite parameter. In this case,... :
S2: = append (S1, 3, 5, 7) // pass S3: = append (S2, s0...) One by one // scatter the Array"
4, goroutine
Goroutine is a language-level support for concurrency in the go language. The granularity of concurrent execution bodies ranges from system to lightweight threads: Processes, threads, and lightweight threads. Goroutine is a lightweight thread. On my computer (win7, Intel i7), you can create about 300,000 goroutine in 3 s.
The biggest problem with concurrency is synchronization and communication. In the go language, synchronization and communication are almost perfectly solved through the channel built in the language. The channel not only communicates with the goroutine, but also achieves synchronization through automatic blocking. The channel can be transmitted as a function parameter. The channel parameter is used as a reference type. It can transmit messages across "functions. However, in fact, it is a value type, and a channel is the boundary of function communication. You cannot use a channel to perform the assignment operation with other side effects. Of course, you have to do this by passing a pointer.
package main import "FMT" func intgenerator (CH Chan INT) {for I: = 0; I <10; I ++ {ch <-I // pass the I to the channel} Close (CH) // close the channel} func main () {var ch Chan Int = make (Chan INT) Go intgenerator (CH) for {Val, OK: = <-ch // If channel closed, the OK will be falseif OK {FMT. println (VAL)} else {return }}