Go Language Beginner Notes-the difference from Python

Source: Internet
Author: User
Tags gopher

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

Go's doing

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 version

Go 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 Main

Import "FMT"

Func Main () {

Fmt. Println ("Hello, World")

}

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 # Generate executable Hello

$ ls

Hello Hello.go

$./hello # Run executable file, run very fast

Hello, World

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."

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 Main

Import "FMT"//introduced FMT package, but not using

Func Main () {

}

Error:

$ go Build hello.go

# command-line-arguments

./hello.go:3:8:imported and not used: "FMT"


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


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


Go annotation method, like most other languages

: Single-line comment

/* * *: Multi-line Comment

General structure of Go program

The package name of the current program

Package Main

To import other packages

Import "FMT"

Declaration and assignment of all variables

var name = "Gopher"

General type of declaration

Type NewType int

Statement of the structure

Type Gopher struct{}

Declaration of the interface

Type Golang interface{}

Initiated by the main function as a program entry point

Func Main () {

Fmt. Println ("Hello world!")

}


. The import of the package

Alias:

Import Std "FMT"

Omit the call (this can be called directly without writing the package name):

Import. "FMT"

Initialized, but not called:

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

2, variables declared inside the function, only valid within 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


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 Main

Import (

"FMT"

".. /test "//Introduce test package

)

Func Main () {

Fmt. PRINTLN (test.name)//Get the NAME variable of the test package

}


Test.go

Package test

var name = "MyName"//variable name uppercase to be referenced by other packages, similar to the public keyword in Java

Run results

Go Run main/main.go

MyName

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 = 1

Const B = "A"

Const (

Text = "123"

length = Len (text)

num = b * 20

)

Define multiple variables at the same time

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 = 1

Const (

b = A

Error at this time because global variables cannot be determined at compile time, constants must be values that can be determined at compile time or obtained using built-in functions (Eg:len ())

)

Const (

A = "a"

B

C

At this point the a,b,c are all "a"

)

Const (

A, B = 1, "a"

C, D

A,c is 1,b,d for "A". Note that the number of constants in the downstream must be consistent with the upstream quantity

)

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

The value of D//d is 3, because 4 variables are defined in the constant group, so the iota counter counts 4 times from 0, so 3

)

4. Operators

High-to-low priority

*/% << >> & &^

=  -  | ^

= = = < <= >= >

&&

||

Example:

/*

6 binary:0110        first

10 binary:1011        second

---------

&  0010  = 2    (two of them are 1 for 1)

|  1111  = 15    (one is 1 is 1)

^  1101  = 13    (two only one is 1 is 1)

$^ 0100  = 4    (the second is 1 is 0, otherwise it is the same as the first bit)

*/

Related Article

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.