This is a creation in Article, where the information may have evolved or changed.
Language history
Golang is an open source programming language developed by Google on the basis of the C language
- 2007 Google's several Daniel began to study
- November 2009, External release
- March 2012 28, the first official version released externally
Environment construction
Golang and Liteide Installation
- After downloading the decompression
Golang download Link recommended 1.7.4 Version
- Configuring Environment variables
Configuring environment Variables goroot and Gopath
- After downloading the decompression
Liteide Download link recommended latest version
- Configuration
Configure Gopath, choose to compile the system platform and edit the corresponding environment for the platform (Focus on Goroot)
Characteristics
- Static compilation
Golang is a statically compiled language, common errors can be found during compilation, compilation of an executable file, general processing glibc outside of the other things, making deployment simple
- built-in powerful toolchain, such as the Gofmt tool
Gofmt can automatically format code, so that go Code style uniform
refers to the code style, the go language for the { } style has strict restrictions, { must be at the end of the line, cannot start another row
- multiple return value
Golang The return value of the support function can be multiple
most commonly used is two return values, the first pass the required specific value, the second is the error type, Used to transfer whether an error occurred during the execution of a function
- anonymous combination implementation inheritance
// golangtest project main.gopackage mainimport ( "fmt")type Base struct { FirstName, LastName string Age float32}func (base *Base) HasFeet() { fmt.Println(base.FirstName + " " + base.LastName + " has feet! Base")}func (base *Base) Flying() { fmt.Println("Base Can flying!")}type Sub struct { Base Area string}func (sub *Sub) Flying() { /* 要调用基类的方法,必须显示调用sub.Base.Flying(),而不是sub.Flying(),否则无限循环调自己 */ sub.Base.Flying() fmt.Println("Sub flying")}func main() { chk := new(Sub) chk.Flying() chk2 := &Sub{Base{"Bob", "Steven", 2.0}, "China"} fmt.Println(chk2.Area) chk2.HasFeet()}
- Non-Intrusive Interface interface
// golangtest project main.gopackage mainimport ( "fmt")type Phone interface { call()}type NokiaPhone struct {}func (nokiaPhone NokiaPhone) call() { fmt.Println("I am Nokia, I can call you!")}type IPhone struct {}func (iPhone IPhone) call() { fmt.Println("I am iPhone, I can call you!")}func main() { var phone Phone phone = new(NokiaPhone) phone.call() phone = new(IPhone) phone.call()}
- Language Level support concurrency
Keywords: go
Golang start a goroutine, just add the keyword go before the function call (maybe this is why the language is called go, and of course some people think that because Google's first two letters are go)
go func(){ fmt.Println("start func") time.Sleep(120*time.Second) }()
- Anonymous functions and closures
- Support Reflection
- Callable C code (CGO)
The problems that I have encountered
- { }
The { } style is strictly restricted in the go language,{ must be at the end of the line, not another row
- Init () function
The init () function is special and cannot be displayed, but is called automatically before the main () function is executed, for the initialization of the package, there can be multiple init () in a package, and there is no specified call order, and the different package init () The order in which the functions are executed is executed by the package import's dependency order
- Defer
The DEFER keyword corresponds to finally in Java, which can have more than one defer in a function before the function exits, executed sequentially in the order in which they were first declared
Common to close various resources and release locks, etc.
- Method
A friend of the newly-Golang may find that some function-defined code, between the Func keyword and the function name , defines (variable name type), and may have a friend to ask.
Q: What does this (variable name type) represent?
A: This definition is similar to a class method in Java, which means that only variables of type change can call this method
- function, variable first letter case
An uppercase letter means that it can be accessed by another package, and lowercase means it can only be accessed within the package
Application example, there is a common error, when the struct definition, if the variable in the struct declaration is lowercase, when the string to the struct will be an error
- New make
The role of new is to initialize a pointer to a type (*T), which is initialized for Slice,map or Chan and returns a reference (T)
Beginner Golang, Welcome to discuss correct
Reference:
http://studygolang.com/articles/6382
http://www.runoob.com/go/go-interfaces.html
/http www.01happy.com/golang-diff-between-new-make/