Go Language Beginner Notes-the difference from Python

Source: Internet
Author: User

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

Image

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 of the blockchain technology is to push the go on the boom. IoT device Connectivity Protocol is also the most of go language development technology. This shows that the future belongs to the world of Go and Python.

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 versiongo version go1.10.3 darwin/amd64

Go Development Editor

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:
A scripting language similar to python,php, running directly, one step

$ go run hello.go

As java,c++, to compile, and then run the compiled output executable file

$ go build hello.go # 生成可执行文件 hello$ lshello    hello.go$ ./hello # 运行可执行文件,运行速度极快Hello, 世界

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 is very overbearing, directly down the command: "Curly braces can only start at the current line, not another row, and another line is pagan, direct compilation Error!" "Let's try it."

Format specification

For another line of obsessive-compulsive programmers, to use 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:

$ go build hello.go# command-line-arguments./hello.go:3:8: imported and not used: "fmt"
Throw error

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.
Go built-in keywords total of 25

Built-in keywords

Go language pre-defined identifiers (case sensitive) total of 38


Pre-defined identifiers

Comment Method for Go

// :单行注释/* */ :多行注释

General structure of Go program

//当前程序的包名package main//导入其它的包import "fmt"//全部变量的声明与赋值var name = "gopher"//一般类型的声明type newType int//结构的声明type gopher struct{}//接口的声明type golang interface{}//由main函数作为程序入口点启动func main() {  fmt.Println("Hello World!")}

Import of packages

别名:import std "fmt"省略调用(这样可以不用写包名,直接调用):import . "fmt"初始化,但不调用:import _ "github.com/go-sql-driver/mysql"

Visibility rules

Only uppercase letters can be called by other packages (similar to object-oriented properties public and private)

Scope

1. Variable declared in code block, only valid within block

Variables declared by code blocks

2, variables declared inside the function, only valid within the function

The change of the internal declaration of the function

3, variables declared outside the function are valid throughout the package. If the variable name is uppercase, it is valid throughout the program

Variables declared outside the function

4, if the variable name to share to other packages, you need to change the package name to uppercase, create the following directory structure:

$ 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 = "myname" // 变量名大写,才能被其他包引用,类似于java中的public关键字

Run results

$ go run main/main.gomyname

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

Constants and operators

1. Definition of constants
The value of the constant is confirmed at compile time
Constants are defined in the same format as variables
The right side of the equal sign must be a constant or constant expression
A function in a constant expression must be a built-in function
Defining a single constant

const a int = 1const b = "A"const (    text = "123"    length = len(text)    num = b * 20)//同时定义多个变量const i, j, k = 1, "2", "3"const (    text2 ,length2, num2 = "456", len(text2),  k* 10)

2. Initialization rules for constants
When defining a constant group, an expression that uses the upstream if the initial value is not provided

var a = 1const (    b  = a    //此时报错,因为全局变量在编译时不能确定值,常量必须是编译时能确定值的或使用内置函数获得的值(eg:len()))const (    a = "A"    b    c    //此时 a,b,c都为"A")const (    a, b = 1, "A"    c, d    //a,c为1,b,d为"A"。此时注意下行的常量个数必须与上行数量保持一致)

3. Enumeration of constants
Using the same expression does not mean that you have the same value
Iota is a constant counter, starting with 0 and automatically incrementing 1 for each of the 1 constants defined in the group.
The effect of enumerations can be achieved by initializing rules with iota
Iota resets to 0 for every const keyword encountered

const (    a = "A"    b    c = iota    d        //d的值为3,因为在常量组中,定义了4个变量所以iota计数器从0开始计数4次,所以为3)

4. Operators

//优先级从高到低     *  /  %  <<  >>  &  &^=  -  |  ^   ==  !=  <  <=  >= >&&||

Example:

 /*6 binary: 0110 The first 10 binary: 1011 Second---------& 0010 = 2 (two are 1 for 1) | 1111 = 15 (one is 1 is 1) ^ 1101 = 13 (two only one is 1) $^ 1 = 0100 (the second is 4 is 1, otherwise it is the same as the first bit) */ 

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.