Go language Learning

Source: Internet
Author: User
Tags install go
This is a creation in Article, where the information may have evolved or changed.

A description of the application scenario

Listen why do you want to learn the go language? Now that more and more famous open source projects are developed using the go language, it is necessary to understand the basics of the go language. In peacetime, the main use of the shell and Python to write operation scripts. I've seen countless introductions and share posts about the go language before. For the Go language, this performance is second only to C and C + +, but also more than the performance of the glue language python. If you need to deal with performance issues later in your work, you can use go to write.


Two projects written in the Go language

Listen to the more famous projects written in the Go language:

Listen to Docker listening to open source containers

Listen to open Falcon Xiaomi's source monitoring tool

Listen to Codis's Redis solution for pea pods, and several components of Codis are written in the Go language

Listen to ected and zookeeper similar tools

Listen to kubernetes Google Open source container scheduling tool



Three Go language basic learning

Install the go language before testing the Go language program.

CentOS uses yum-y install go installation

1. Print Hello world!

Package Listen Mainimport Listen to "FMT" Func Listen to Main () {Listen and listen to the FMT. Println ("Hello Listen world!") Listen, listen, listen, hear, hear, listen.
#聽go聽run聽hello-world.go Listen to world! #聽go聽build聽hello-world.go listen to listen to Lshello-world listen to hello-world.go# listen to. Hello-world Listen to Hello listen to world!


2. Assignment type

Package listen to Mainimport listen to "FMT" Func Listen to Main () listen {Listen and listen to the FMT. Println ("Go" listen + listen "Lang") listen and listen to the FMT. Println ("Listen =", "+") listen and listen to the FMT. Println ("7.0/3.0 listen =", 7.0/3.0) listen and listen to the FMT. PRINTLN (True&&false) Listen and listen to the FMT. Println (true| | FALSE) Listen and listen to the FMT. PRINTLN (!true) Listen, listen, hear, hear, hear, listen.
#聽go聽run聽values. Go Listen golang1+1 = listen to 27.0/3.0 listen = listen to 2.3333333333333335falsetruefalse


3. Variables

Package Listen Mainimport Listen to "FMT" Func Listen to Main () listen {Listen to listen to the Var listen to a listen to string listen = Listen to "initial" listen to listen to the FMT. Println (a) listen and listen listen to the Var listen to b,c listen int listen = Listen to listen to hear the FMT. PRINTLN (B,C) listen and listen to Var listen to D=true listen and listen to the FMT. Println (d) Listen and listen to the Var listen e listen int listen to listen to the FMT. PRINTLN (e) Listen and listen to the F-listen: = Listen to "short" and listen to the FMT. Println (f)}
#聽go聽run聽variable. Go Listen initial1 listen to 2true0short


4. Constants

Package listen to Mainimport listen to "FMT" Import Listen to "math" Const listen to listen to string listen = Listen to "constant" Func listen to Main () listen {Listen to listen to the FMT. PRINTLN (s) Listen listen to listen to listen to hear the const listen n listen = Listen to hear listen to listen to listen to listen to hear a const listen to listen to listen to hear the 3e20 listen to N/a listen to listen to the FMT. Println (d) Listen and listen to the FMT. PRINTLN (Int64 (d)) listen and listen and listen to the FMT. Println (Math. Sin (n))}
Listen to go listen to run listen to Constant.go listen to constant6e+11600000000000-0.2847040732381611


5.for Cycle

The For statement is the only loop statement in the Go language

Package listen to Mainimport listen to "FMT" Func Listen to Main () listen {Listen to listen to I listen to: =1 listen to listen to I listen to <= listen to hear the 3 listen to listen to listening to listen to listening to the FMT. PRINTLN (i) listen to listen to listen to hear I listen to listen to i+1 listen to listen to listening to hear for listen to listen to listening to: =7;j listen to listen to the <= listen to 9, listen to J + + Listen {Listen to listen to hear listen to listen to the FMT. Println (j) Listen and listen to listen and hear for listening to listen to hear and hear the FMT. Println ("loop") listen to listen and listen to the break listen to hear}}
#聽go聽run聽for. Go Listen 123789loop

6.if/else statements

The package listens to Mainimport listen to "FMT" Func Listen to Main () listen {Listen to listen to listen to listen to listen to listen to listening to 7%2 listen to listen to the 0 listen to listen to listening to and listen to the FMT. Println ("7 Listen to is listen to even") listen and listen to}else{listen and listen to the FMT. Println ("7 Listen to is listen to odd") listen and listen listen to listen to hear if listen to listen to hear the 8%4 listen to listen to 0 listen to listen to hear (listen to listening to listen to listen to listen to the FMT. PRINTLN ("8 listen to is listen to divisible hear by 4") listen and listen to listen to hear if listen to num listen to: = Listen to the 9;num listen to < Listen to the 0 listen to listen to hear, listen, listen, listen to the FMT. PRINTLN (num, "is listen negative") listen to listen to listen to}else listen to num listen to < Listen to 10 listen {Listen, listen, listen and listen to the FMT. PRINTLN (num, "has heard 1 listen to digit") listen to listen to hear the}else listen to listen to hear and listen listen to the FMT. PRINTLN (num, "has heard multiple listen to digits") Listen to Listen}}

Note here that the statement of the Else statement cannot be written

}

else {

Must be written}else {This form

#聽go聽run聽if. Go Listen 7 Listen is listen ODD8 listen to divisible hear by listen to 49 listen to have heard 1 listen to digit



7.switch statements

Package listen to Mainimport listen to "FMT" Import Listen to "Time" Func listen to Main () listen to listen to hear I listen to: =2 listen to listen to the FMT. Print ("Write", I, "listen as") listen to the switch listen I listen {Listen to hear case listen to listen to 1: Listen, listen, listen, listen and listen to the FMT. Println ("one") listen and listen to the case listen to listen to 2: Listen to listen and listen to the FMT. Println ("both") listen and listen to the case listen to listen to 3: Listen to listen and hear listen to the FMT. Println ("three") listen to listen and listen to the switch listen to time. Now (). Weekday () Listen {Listen and listen to the case listen to time. Saturday, listen to time. Sunday: Listen, listen, listen, listen and hear the FMT. Println ("It's listening to the weekend") listen and listen to the default: Listen, listen, listen and listen to the FMT. Println ("It's listening to a listen to weekday") listen and listen to listen to hear t listen to hear: = Listen to time. Now () listen and listen to switch{listen to the case hearing T. Hour () Listen < Listen 12: Listen, listen, listen, listen and listen to the FMT. Println ("It's listening to before listen to noon") listen and listen to the default: Listen, listen, listen, listen and hear the FMT. Println ("It's listening after listening to Noon") Listen and Hear}}
#聽go聽run聽switch. Go Listen write2 listen to Twoit ' s listen to a listen to Weekdayit ' s listen to after listening to noon


8. Arrays

The package listens to Mainimport listen to "FMT" Func Listen to Main () listen {Listen to listen to listen to listen to listen to listen to hear the Var listen to listen to [5]int listen listen to the FMT. Println ("EMP:", a) listen and listen to a[4] Listen = Listen to 100 listen and listen to the FMT. Println ("Set:", a) listen and listen to the FMT. Println ("Get:", a[4]) listen and listen to B listen: = Listen to [5]int{1,2,3,4,5} listen and listen to the FMT. Println ("DC1:", b) listen to and listen to the Var listen to Twod listen to [2][3]int listen to listen to I listen to: =0;i Listen < Listen to listen to listen to listening to hear listen to hear for listen J listen to listen to listening to: = 0; Listen to J Listen < listen 3; Listen, listen, listen, hear, hear, listen, twod[i][j, listen, hear, hear, listen to, or listen to, listen, hear, listen, listen, listen and listen to the FMT. Println ("2d: Listen", TWOD)}
#聽go聽run聽arrays. Go listen emp: Listen [0 listen to 0 listen to 0 listen to 0 listen to 0]set: Listen to [0 listen to 0 listen to 0 listen to 0 listen to 100]get: Listen to 100DC1: listen to [1 listen to 2 listen to 3 listen to 4 listen to 5]2d: Listen [[0 Listen 1 hear 2] listen [1 Listen 2 hear 3]]



9. Slicing







Resources:

https://gobyexample.com/


This article is from the Linux SA John blog, so be sure to keep this source http://john88wang.blog.51cto.com/2165294/1783291

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.