2018 Go Language Combat Note 1

Source: Internet
Author: User

With the Golden key born go

In recent years, in the programming language, the most fire is go.

Go was born in Google and was written by a master of the following three-bit computer fields

Go's pro-Dad!

With a pedigree, go has attracted a lot of attention from developers since its inception. Since the birth of 10 years, there have been many applications based on go, there has been rumors that go in the future is to replace the current Java location. For a new language with a history of only 10 years, Go's development momentum is quite rapid.

The foreign Docker is to write with go, the domestic seven cattle team widespread use go. Now in full swing blockchain technology is to push the go on the boom

Sounds like such a cool technique, teenager, don't you want to learn?

Go Environment Building

Download the go installation package on the website

If it is a Mac, it can be installed directly with brew:

brew intall go

View Go version

  go [master]  go versiongo version go1.10.2 darwin/amd64

For the editor, it is recommended to use the Golang produced by JetBrains

First GO Program

Run the first go program according to the website demo

New Hello.go File

package mainimport "fmt"func main() {    fmt.Println("Hello, 世界")}

There are two ways to run go:

    1. A scripting language similar to python,php, running directly, one step
  demo [master]  go run hello.go                                                    Hello, 世界
    1. As java,c++, to compile, and then run the compiled output executable file
  demo [master]  go build hello.go # 生成可执行文件 hello  demo [master]  lshello    hello.go  demo [master]  ./hello # 运行可执行文件   Hello, 世界
Code parsing

Go is a language of grammatical cleanliness, such as the imposition of curly braces. For a long time, whether the curly brace is going to be a different line is a holy war in the programmer world, and this jihad crosses the language, operating system, editor. The two sides fought countless rounds over the years, dead heat. The father of Python, Guido van Rossum, directly cancels the curly braces, completely indented, bypassing jihad. In contrast, go appears to be very overbearing, directly under the command of death:

"Curly braces can only start on the current line, not another line, another line is pagan, direct compilation Error!" "

Let's try it out:

Curly Brace Error

For another line of obsessive-compulsive programmers, to go, only to yield.

In addition, if there are redundant variables in the program, or the introduction of unused packets, will be an error

package mainimport "fmt" // 引入了fmt包,但没有使用func main() {}

Error:

  demo [master]  go build hello.go# command-line-arguments./hello.go:3:8: imported and not used: "fmt"
Do not use the variable declaration after

For programmers who have been transferred from other languages, it is not customary to go in the first rule, especially my big PHP programmer. PHP specification is the name of the loose, casually write the line, the programmer is very cool when writing code, how happy how to come, but to maintain the code of others feel very disgusting.

Go's design concept is both concise and rigorous, with mandatory rules to ensure the consistency of the code.

Variable

Go defines the specification of variables, some of which are anti-human. C + +, Java all declares the data type before the variable name, while Go is ingenious to place the data type declaration behind the variable name.

Naming rules

Naming rules for variables: start with a letter or underscore, and are case sensitive. Can't use Go's reserved keyword

Go reserved Keywords

Scope

    1. A variable declared in a code block, valid only within a block
Variable scope
    1. A variable declared inside a function that is valid only within a function
function Internal variables
    1. Variables declared outside the function are valid throughout the package. If the variable name is uppercase, it is valid throughout the program
In-Package global variables

If the variable name is to be shared to another package, you need to change the package name to uppercase

Create the following directory structure:

  demo [master]  tree -L 2.├── main│   └── main.go└── test    └── test.go

Main.go

package mainimport (    "fmt"    "../test" // 引入test包)func main() {    fmt.Println(test.NAME) // 获取test包的NAME变量}

Test.go

package testvar NAME = "章鱼喵" // 变量名大写,才能被其他包引用,类似于java中的public关键字

Results:

  demo [master]  go run main/main.go章鱼喵

You can try to change the name in test to Name,go error, the lowercase variable is the private variable of the module, other modules cannot reference

Constant

Constants use const keywords to indicate that once a constant is defined, it can no longer be changed. Constants are therefore only available for Boolean, numeric (integer, float, and plural) and string literals.

package mainimport "fmt"const name = "章鱼喵"const say = "喜欢我的文章,点个赞吧,或者赏杯咖啡钱"const year = 2018func main() {    fmt.Println(name)    fmt.Println(say)    fmt.Println(year)}

This writing is very verbose and can be used in a more concise way:

package mainimport "fmt"const (    name = "章鱼喵"    say = "喜欢我的文章,点个赞吧,或者赏杯咖啡钱"    year = 2018)func main() {    fmt.Println(name)    fmt.Println(say)    fmt.Println(year)}
  demo [master]  go run main/main.go章鱼喵喜欢我的文章,点个赞吧,或者赏杯咖啡钱2018

Data type

The data types of Go are divided from large classes: Boolean, numeric type, string type, derived type

For specific descriptions of each type, please consult the Go language data type yourself

Go is a strongly typed language, each variable is given a type, and if not specified, go automatically infers the data type of the variable based on the value of the variable at compile time.

The types of variables cannot be changed, the vast majority of PHP developers have to remember that PHP is a weakly typed language, the same variable, you can switch the stored data type at any time. PHP variables are not like a universal basket, anything can be installed. And the go variable is dedicated to the special device

package mainimport "fmt"func main() {    var name string = "章鱼喵" // 指定了数据类型为string    var say = "智慧如你,有什么要说的,要评论区发表吧..." // 可省略数据类型    //say = 2018 // 编译报错:不能篡改类型    fmt.Println(name)    fmt.Println(say)}
  demo [master]  go run main/main.go章鱼喵智慧如你,有什么要说的,要评论区发表吧...

Value types and reference types

    1. Value type: The variable stores the data itself, such as the basic data type: String, Boolean, number, etc.

    2. Reference type: A variable stores the memory address where the data resides, such as pointers, maps, Chan, etc.

package mainimport "fmt"func main() {    var content = "跟章鱼喵一起学go吧..." // 值类型    var pipe = make(chan int, 1) // 引用类型    fmt.Println(content) // 存储具体数据    fmt.Println(pipe) // 存储内存地址}
  demo [master]  go run main/main.go跟章鱼喵一起学go吧...0xc42005a070

Summary

Notes Summary

If you feel that this article is helpful, like, or a cup of coffee money, your approval is important to me

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.