The basics of the Go Language starter Tutorial QuickStart _golang

Source: Internet
Author: User
Tags constant sin

The go language is an open source language designed to create simple, fast, and reliable software.

Go Real (example) tutorial, through an example annotated way to introduce the use of the going language.

Hello World

The first program prints "Hello World" messages. The source code is as follows:

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
Fmt. Println ("Hello World")
}

Run the Go program by going run
$ go Run hello-world.go
Hello World
It takes a while to compile the source code into a binary file that can be implemented through the go build.
$ go Build hello-world.go
$ ls
Hello-world Hello-world.go
Then run the binaries directly
$./hello-world
Hello World

Values: Value types

Go has many value types including: strings, integers, floats, booleans, etc. Here are some examples of the basics.

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {

Strings can be connected by using the +
Fmt. Println ("Go" + "lang")

Integral type and floating point numbers
Fmt. Println ("1+1 =", 1+1)
Fmt. Println ("7.0/3.0 =", 7.0/3.0)

Boolean type, you can use the Boolean operator
Fmt. Println (True && false)
Fmt. Println (True | | false)
Fmt. Println (!true)
}

$ go Run values.go
Golang
1+1 = 2
7.0/3.0 = 2.3333333333333335
False
True
False

Variables: Variable

In the go language, variables are explicitly declared, and the editor can check the correctness of the type in a function call.

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
Declaring one or more variables with Var
var a string = "initial"
Fmt. Println (a)

You can declare multiple variables
var b, C int = 1, 2
Fmt. Println (b, c)

Go will use the default value to determine the variable type
var d = True
Fmt. Println (d)

Variables with a type but no value are assigned a zero value of zero-valued. The 0 value of the int type is 0.
var e int
Fmt. Println (e)

: = language is shorthand for declaring and initializing variables, such as
With: var f string = "short" equivalence
F: = "short"
Fmt. Println (f)
}

$ go Run variables.go
Initial
1 2
True
0
Short

Constants: Constants

Go-supported constants have character, String, Boolean, and numeric types.

Copy Code code as follows:

Package Main

Import "FMT"
Import "Math"

Constants declared with const
Const s String = "constant"

Func Main () {
Fmt. PRINTLN (s)

The const declaration can be used anywhere, like var.
Const N = 500000000

Constants can represent arbitrary precision
Const D = 3e20/n
Fmt. Println (d)

Numeric constants have no type until they are assigned, as specified by the following display
Fmt. PRINTLN (Int64 (d))

A number can specify the type you want in the context, such as math. Sin needs a float64 type.
Fmt. Println (Math. Sin (n))
}

$ go Run constant.go
constant
6e+11
600000000000
-0.28470407323754404

For: Looping

For is the only loop structure in the go language. The following are the basic three types of loops.

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {

The most basic type, a single loop
I: = 1
For I <= 3 {
Fmt. Println (i)
i = i + 1
}

A classic type: loop with initialization/condition
For j: = 7; J <= 9; J + + {
Fmt. Println (j)
}

The For loop that passes the condition is executed until you break or return in the closure.
for {
Fmt. Println ("Loop")
Break
}
}

$ go Run for.go
1
2
3
7
8
9
Loop


In the range syntax, we'll also see another for use.

IF/ELSE: Conditional statement

Go through if and else to implement conditional branching

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
A basic usage
If 7%2 = 0 {
Fmt. Println ("7 is even")
} else {
Fmt. Println ("7 is odd")
}

Only if the case
If 8%4 = 0 {
Fmt. Println ("8 is divisible by 4")
}

You can declare a variable in a condition; any declaration can be used in all conditional code snippets
If num: = 9; Num < 0 {
Fmt. PRINTLN (num, "is negative")
else if num < 10 {
Fmt. PRINTLN (num, "has 1 digit")
} else {
Fmt. PRINTLN (num, "has multiple digits")
}
}


There is no triple-ternary if operator in the go language, you need to use if to implement
Copy Code code as follows:

$ go Run if-else.go
7 is odd
8 is divisible by 4
9 has 1 digit

Switch: Condition Enumeration

Switch syntax allows you to implement multiple conditional branches.

Copy Code code as follows:

Package Main

Import "FMT"
Import "Time"

Func Main () {
Here is a basic switch
I: = 2
Fmt. Print ("Write", I, "as")
Switch I {
Case 1:
Fmt. Println ("one")
Case 2:
Fmt. Println ("two")
Case 3:
Fmt. Println ("three")
}

You can use a variety of conditions to match, and also you can use the default matching
Switch time. Now (). Weekday () {
Case time. Saturday, time. Sunday:
Fmt. Println ("It ' s The weekend")
Default
Fmt. Println ("It ' s a weekday")
}

Unconditional switch can achieve the If/else type effect
T: = time. Now ()
Switch {
Case T.hour () < 12:
Fmt. Println ("It ' s before noon")
Default
Fmt. Println ("It ' s after noon")
}
}

$ go Run switch.go
Write 2 as two
It ' s the weekend
It ' s before noon

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.