Tip: This series is for readers with a persistent urge to Go

At the beginning of a, see

China’s official website golang.google.cn/

1. Linux binary installation
#Get go1.16.4Wget https://golang.google.cn/dl/go1.16.4.linux-amd64.tar.gz tar - xf go1.16.4. Linux - amd64. Tar. Gz - C/usr/local echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile source /etc/profile
#Verify the installation
[root@VM-0-5-centos bin]# go version
go version go1.16.4 linux/amd64


Copy the code

2.Hello, Water Man!

vim water_man.go

package main

import "fmt"

func main(a) {
    fmt.Println("Hello, Water Man!")}Copy the code

go run water_man.go

Hello, Water Man!
Copy the code

Go applications directly compile target platform binary executables

[root@VM-0-5-centos hellogo]# go build water_man.go 
[root@VM-0-5-centos hellogo]# ls
water_man  water_man.go
[root@VM-0-5-centos hellogo]# ./water_man 
Hello, Water Man!
Copy the code

2. Learn about GO

1. go module

Golang’s package management tool.

Package design is intended to facilitate modular design and use. In Go, a module is a collection of packages. If a module needs to be used by others, it should be named with a downloadable Web address when go Mod init is initialized.

Initialize the rush_B module

mkdir rush_b && cd rush_b

[root@VM-0-5-centos rush_b]# go mod init github.com/csgo/rush_b
go: creating new go.mod: module github.com/csgo/rush_b

#Get the go.mod file that contains the package list name and the GO version number
[root@VM-0-5-centos rush_b]# ls
go.mod
Copy the code

Create rush_b. Go Write module implementation functions. The module functions used for sharing should start with uppercase letters.

package rush_b

import "fmt"

func Rush(name string) string{
  message := fmt.Sprintf("%v, Follow me!",name)
  return message
}

Copy the code
  • The function definition in GO needs to place the declared type after it.

  • := is equivalent to defining and assigning at the same time, which is shorthand for the following

    var message string
    message = fmt.Sprinf("%v,Follow me!",name)
    Copy the code

Once go.mod and rush_b. Go are created, our github.com/csgo/rush_b module is ready, and it’s time to think about how to reference it.

Similarly, let’s create a water_man folder

[root@VM-0-5-centos hellogo]# ll water_man/ total 8 -rw-r--r-- 1 root root 32 May 11 20:49 go.mod -rw-r--r-- 1 root root  169 May 11 20:54 water_man.goCopy the code

Import the rush_B module and use its rush function.

package main

import (
    "fmt"
    "github.com/csgo/rush_b"
)
func main() {
    fmt.Println("Hello")
    message :=rush_b.Rush("water man")
    fmt.Println(message)
}    
Copy the code

After go run, we will download the module according to the address. Since the module is a local module, we go mod edit –replace=github.com/csgo/rush_b=.. / rush_B tells GO to use local modules.

Then, go run normally calls the Rush function of rush_B module.

2. Errors and log Are used to standardize error messages and log troubleshooting.

An error is returned when the Rush function is called with an empty entry parameter.

//rush_b.go
package rush_b

import (
        "fmt"
        "errors"
)

func Rush(name string) (string,error){
  if name == ""{
    return "",errors.New("empty name")
  }
  message := fmt.Sprintf("%v, Fllow me!",name)
  return message,nil
}


//water_man.go
package main

import (
    "fmt"
    "log"
    "github.com/csgo/rush_b"
)
func main(a) {
    //fmt.Println("Hello")
    log.SetPrefix("logging:")   // Set the prefix
    log.SetFlags(0)
    message,err := rush_b.Rush("")
    fmt.Println(message)
    iferr ! =nil{
      //fmt.Println("it is an err")
      log.Fatal(err)   / / print error
    }
}  

[root@VM0- 5-centos water_man]# go run water_man.go 
logging:empty name
exit status 1

Copy the code
3. Let S1mple rush B with me

Rush_b. Go returns a random slogan in the ADDR slice when Rush is called.

package rush_b

import (
        "fmt"
        "errors"
        "math/rand"
        "time"
)

func Rand_init(a){  // Random number seed
   rand.Seed(time.Now().UnixNano()) 
}
func RandomFormat(a) string {
    addr :=[]string{
      "%v ,follow me rush A"."%v ,follow me rush B"."%v ,follow me rush dust2",}return addr[rand.Intn(len(addr))]   //len and Intn return an integer [0,n]
}
func Rush(name string) (string,error){
  if name == ""{
    return "",errors.New("empty name")
  }
  message := fmt.Sprintf(RandomFormat(),name)
  return message,nil
}

Copy the code

water_man.go

package main

import (
    "fmt"
    "log"
    "github.com/csgo/rush_b"
)
func main(a) {
    //fmt.Println("Hello")
    rush_b.Rand_init()    // Initialize the seed
    log.SetPrefix("logging:")
    log.SetFlags(0)
    message,err := rush_b.Rush("s1mple")  / / incoming S1mple
    fmt.Println(message)
    iferr ! =nil{
      //fmt.Println("it is an err") 
      log.Fatal(err)
    }
}

Copy the code

Go run water_man. Go rush

[root@VM-0-5-centos water_man]# go run water_man.go 
s1mple ,follow me rush dust2
[root@VM-0-5-centos water_man]# go run water_man.go 
s1mple ,follow me rush B
[root@VM-0-5-centos water_man]# go run water_man.go 
s1mple ,follow me rush dust2
[root@VM-0-5-centos water_man]# 
Copy the code
4. Get top3 to feint

Maps are used, similar to dictionaries in Python

// rush_b.go
package rush_b

import (
        "fmt"
        "errors"
        "math/rand"
        "time"
)

func Rand_init(a){
   rand.Seed(time.Now().UnixNano()) 
}
func RandomFormat( name string) (string,error) {
  if name == ""{
    return "",errors.New("empty name")
  }
  addr := []string {

    "%v ,follow me rush A"."%v ,follow me rush B"."%v ,follow me rush dust2",}return fmt.Sprintf(addr[rand.Intn(len(addr))],name),nil
}


func Yg(names []string ) (map[string]string,error){   / / feint
  messages := make(map[string]string)    // Map initialization
  
  for _,name := range names{
    message,err := RandomFormat(name)
    iferr ! =nil {
      return nil, err
    }
    messages[name] = message
  } 
  return messages,nil
}


//water_man.go
package main

import (
    "fmt"
    "log"
    "github.com/csgo/rush_b"
)
func main(a) {
    //fmt.Println("Hello")
    rush_b.Rand_init()
    log.SetPrefix("logging:")
    log.SetFlags(0)
    hero:=[]string{"s2mple"."zywoo1"."device"}
    message,err := rush_b.Yg(hero)
    for i := 0; i < len(hero); i++ {
      fmt.Println(message[hero[i]])
    }
    
    iferr ! =nil{
      //fmt.Println("it is an err") 
      log.Fatal(err)
    }
}
Copy the code

Go Run water_man.go The command output is as follows:

[root@VM-0-5-centos water_man]# go run water_man.go 
s2mple ,follow me rush B
zywoo1 ,follow me rush A
device ,follow me rush A
Copy the code

Reference book: Go Language Bible

Feel free to point out any shortcomings in the comments section.

Favorites, likes and questions are welcome. Follow the top water cooler managers and do something other than pipe hot water.