Go Language _ Basic Learning Chapter

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

Go file directory structure

The graph is the directory structure of the Go-windows

, go program should be under the Gopath folder, divided into bin, pkg, and src three sub-folders

src folder : Each sub-folder (such as demo) represents a Go project, which stores the source program

Bin folder : Binary executable program generated for each project of go. The. exe file is generated under Windows and executable files are generated under Linux. One of the biggest features of Go is portability, which means that when a demo.exe is generated, it can be executed on any Windows system (even if the go installation package is not installed). This is to let Php,python and other scripting language look mo.

pkg Folder : third-party libraries. It contains the third-party libraries referenced in your project (unofficially provided libraries).

Go basic knowledge

Variable assignment: (in the following form, set a to int 12)

var AA = 12a: = 12a,b: =12,23

Constant assignment:

1 Itoa Use

Const (A = Iotab)

Iota is the constant assignment increment from go, the above code sets a to 0,b to 1

2 string

String in Go is a constant and can only be represented by double quotation marks.

A: = "This is a string"

A[0] = ' C ' (this is wrong, will be an error)

If you want to do this, you should do the following:

A: = "This is string" c: = []bytes (a) c[0] = ' C ' d: = string (c)

3 Cycle and selection structure

There is no do while in go, and the loop structure is for only. Select structure with IF and switch

If statement

If err: = file. Chmod (777); Err! = Nil {return err}

Tangent: The opening parenthesis must be on the same line as if, so

If err: = file. Chmod (777); Err! = nil{//This is illegal in go statement return err}

For loop

Sum: = 0for I: = 0; I < 10; i++ {sum + = i}list: = []string{"AAA", "BBB", "CCC"}for _,v: = Range list{fmt. Print ("%s", V)}

A range,key and value are used here, respectively, int and string, key is ordinal, starting from 0, value is

Switch statement

switch is similar to other languages, only one, it does not need to break, query to a satisfying condition, execute, and then jump out

Switch a {case "test1": FMT. Print ("test1") case "Test2", "test3": FMT. Print ("Testohter") default:fmt. Print ("Notest")}

Array, slice, and map

Array is the usual arrays, and the C language is the same as the array

var a [10]inta[0] = 1a[1] = 2

Two-dimensional arrays:

A: = [2][2]int{{1,2}, {3,4}}

Slice and array close, slice understood as a pointer to array, use make for memory allocation

SL: = Make ([]int, 10)

The description of the conversion of array and slice uses an excerpt from "Learning Go Language":

You want to extend the Slice,append and copy two built-in functions.

PS: The difference between a built-in function and a third-party library function is that the built-in function is preceded by a lowercase letter, such as copy (), and the third-party library function is capitalized, such as FMT. Print ())

The map structure is a hash map

Ages: = map[string]int {"Lili": +, "Nick": +, "Jacky": 55,}

Note here that in many languages, the last comma often requires omitting (comma after 55), but this comma is required in the go language.

Exercises:

1 Create a simple loop based on for. Make it loop 10 times, and print out the value of the counter using the FMT package.

2 using Goto to modify the 1 loop, do not use the for

3 rewrite the 1 loop again, make it traverse an array, and print the array to the screen

4 Write a program to reverse the string, for example: "Foobar" Printing becomes "raboof";

To post my answer:

(For the fourth question, there are a number of solutions, more detailed see: http://stackoverflow.com/questions/1752414/how-to-reverse-a-string-in-go)

Package Mainimport ("FMT") func main () {forexample () fmt. Println ("----------------------") goexample () fmt. Println ("----------------------") arrexample () fmt. Println ("----------------------") Revert ("Testrevert hah")}func Forexample () {for i:= 0; i <; i++ {FMT. Println (i)}}func goexample () {i: = 0i:fmt. Println (i) i++if (i <) {goto I}}func arrexample () {arr: = [10]int{0,1,2,3,4,5,6,7,8,9}for _,val: = Range arr{fmt. Println (val)}}func Revert (s string) {var result stringfor _,val: = Range S{result = string (val) + result}fmt. PRINTLN (Result)}

Reference articles

Learning the Go language

How-to-revese-a-string-in-go

"Effective_go"

This article is based on the attribution-Non-Commercial use 3.0 license Agreement published, welcome reprint, deduction, but must retain this article's signature Ye Jianfeng (contains link http://www.cnblogs.com/yjf512/), and not for commercial purposes. If you have any questions or authorization to negotiate, please contact 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.