Go quick start

Tip: this series of articles is suitable for readers who have other phonetic foundations and have a continuous impulse to Go

1, package introduction

The code of Go language is organized through package. The concept of package is similar to the concepts of libraries or modules in other languages you know.

A package will contain one or more go ends the source code file. Each source file starts with a declaration statement of package xxx. For example, in our example, package main. This line of declaration statement indicates which package the file belongs to, followed by a series of import ed package names, indicating the packages introduced in the file. Then there is the code of this document itself.

Package is a special package. An independent program will be defined in this package, which can be run instead of corresponding to a library like other packages.

This is precisely because the go language must introduce the principle of all packages to be used. If you do not import the packages to be used in the code, the program will not be able to compile. At the same time, when you import the packages that are not used, it will not be able to compile.

2, Command line parameters

The os package provides some functions and related variables that are independent of the operating system (cross platform) and interact with the system. The command line parameters of the runtime program can be obtained through a variable called args in the os package; When using this variable outside the os package, you need to use os Args to access.

os. The first element of args, OS Args [0] is the command itself when the command line is executed; Other elements are the parameters passed to the program when the command is executed. In the slice expression mentioned earlier, s[m:n] will return the m-th to n-1 st elements, so the OS. 0 is needed in the next example Args [1: len (os.Args)] is all the parameters passed in except the command itself. If we omit m and N in s[m:n], the default expression will be filled with 0:len(s), so here we can also omit N and write it as OS Args[1:].

1. Output command line parameters
// Use of command line parameters in go
package main

import (
	"fmt"
	"os"
)

func main() {
	var s, seq string
	seq = " "
	for i := 1; i < len(os.Args); i++ {
		s += os.Args[i] + seq
	}
	fmt.Println(s)
}

[root@VM-0-5-centos course1]# go run lesson-1.go  hello world
hello world
2. About circulation

In Go language, there is only a for loop.

for initialization; condition; post {
    // zero or more statements
}

// a traditional "while" loop
for condition {
    // ...
}

// a traditional infinite loop
for {
    // ...
}

range

Each cycle iteration, range generates a pair of values; Index and the element value at that index. This example does not need an index, but the syntax of range requires that the index must be processed in order to process elements. One idea is to assign the index to a temporary variable, such as temp, and then ignore its value, but Go language does not allow the use of useless local variables, because this will lead to compilation errors.

The solution to this situation in Go language is to use blank identifier, i.e_ (i.e. underline). An empty identifier can be used when the syntax requires a variable name but the program logic does not. For example, in a loop, discard the unnecessary loop index and retain the element value. Most Go programmers use range and like this_ Write the echo program because it implicitly rather than explicitly indexes OS Args, easy to write right.

package main

import (
	"fmt"
	"os"
)

func main() {
	var s string
	seq := " "

	for _, arg := range os.Args[1:] {
		s += arg + seq
	}
	fmt.Println(s)
}

strings.Join connection parameters

package main

import (
	"fmt"
	"os"
	"strings"
)

func main() {
	var s string
	s = strings.Join(os.Args[1:], " ")
	fmt.Println(s)
	fmt.Println(os.Args[1:])   //Slice output
}

[root@VM-0-5-centos course1]# go run join.go wo apple ibm cisco
wo apple ibm cisco
[wo apple ibm cisco]  

3, Practice

Exercise 1.1: modify the echo program to print the index and value of each parameter, one line at a time.

package main

import (
	"fmt"
	"os"
)

func main() {
	var s string
	seq := " "

	for index, arg := range os.Args[1:] {
		s += arg + seq
		//fmt.Println("yees" + s)
		fmt.Println(index, arg)
	}
	//fmt.Println(s)
}

Exercise 1.2: modify the echo program to print OS Args [0], the name of the executed command itself.

package main

import (
	"fmt"
	"os"
)

func main() {
	var s string
	seq := " "

	for index, arg := range os.Args[1:] {
		s += arg + seq
		//fmt.Println("yees" + s)
		fmt.Println(index, arg)
	}
	fmt.Println(os.Args[0])
}
//output
[root@VM-0-5-centos course1]# go run for_range.go  apple ibm
0 apple
1 ibm
/tmp/go-build844979177/b001/exe/for_range

Exercise 1.3: do experiments to measure potentially inefficient versions and use strings Run time difference between versions of join. (time package)

//for loop traversal
package main

import (
	"fmt"
	"os"
	"time"
)

func main() {
	var s, seq string
	seq = " "
	var start_time = time.Now()
	for i := 1; i < len(os.Args); i++ {
		s += os.Args[i] + seq
	}
	fmt.Println(s)
	end_time := time.Now()
	fmt.Println(end_time.Sub(start_time))
}

[root@VM-0-5-centos course1]# go run comandline.go  apple ibm
apple ibm 
19.667µs

//join connection
package main

import (
	"fmt"
	"os"
	"strings"
	"time"
)

func main() {
	var s string
	start_time := time.Now()
	s = strings.Join(os.Args[1:], " ")
	fmt.Println(s)
	//fmt.Println(os.Args[1:])
	end_time := time.Now()
	fmt.Println(end_time.Sub(start_time))
}

[root@VM-0-5-centos course1]# go run join.go  apple ibm
apple ibm
16.782µs


Book reference: Go language Bible Chinese version

If there are deficiencies in the article, please point them out in the comment area.

Welcome to collect, like and ask questions. Pay attention to the top water dispenser administrators. In addition to heating hot water, they sometimes do something else.

Keywords: Go

Added by Robban on Tue, 08 Feb 2022 19:40:41 +0200