提示:本系列文章适合有其他语音基础并对Go有持续冲动的读者

一、见初

国内官网https://golang.google.cn/

1.linux二进制安装
#获取go1.16.4
wget 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

#验证安装
[[email protected] bin]# go version
go version go1.16.4 linux/amd64


2.Hello, Water Man!
water_man.go
package main

import "fmt"

func main() {
    fmt.Println("Hello, Water Man!")
}
water_man.go
Hello, Water Man!

go应用可直接编译目标平台二进制可执行文件

[[email protected] hellogo]# go build water_man.go 
[[email protected] hellogo]# ls
water_man  water_man.go
[[email protected] hellogo]# ./water_man 
Hello, Water Man!

二、了解go

1. go module

golang的包管理工具。

go mod init
rush_b
mkdir rush_b && cd rush_b

[[email protected] rush_b]# go mod init github.com/csgo/rush_b
go: creating new go.mod: module github.com/csgo/rush_b

#获得包含包列表名和go版本号的go.mod文件
[[email protected] rush_b]# ls
go.mod
rush_b.go
package rush_b

import "fmt"

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

var message string
message = fmt.Sprinf("%v,Follow me!",name)
go.modrush_b.gogithub.com/csgo/rush_b

同理我们创建一个water_man文件夹

[[email protected] 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.go

导入rush_b模块,使用其中的rush函数。

package main

import (
    "fmt"
    "github.com/csgo/rush_b"
)
func main() {
    fmt.Println("Hello")
    message :=rush_b.Rush("water man")
    fmt.Println(message)
}    
go mod edit --replace=github.com/csgo/rush_b=../rush_b
go run
2.errors与log 用于规范错误提示和日志排查。

当调用的Rush函数入参为空时返回错误。

//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() {
    //fmt.Println("Hello")
    log.SetPrefix("logging:")   //设置前缀
    log.SetFlags(0)
    message,err := rush_b.Rush("")
    fmt.Println(message)
    if err !=nil{
      //fmt.Println("it is an err")
      log.Fatal(err)   //打印error
    }
}  

[[email protected] water_man]# go run water_man.go 
logging:empty name
exit status 1

3.让S1mple跟我rush B
rush_b.go
package rush_b

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

func Rand_init(){  //随机数种子
   rand.Seed(time.Now().UnixNano()) 
}
func RandomFormat() 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函数、Intn方法返回一个[0,n)的整数
}
func Rush(name string) (string,error){
  if name == ""{
    return "",errors.New("empty name")
  }
  message := fmt.Sprintf(RandomFormat(),name)
  return message,nil
}

water_man.go
package main

import (
    "fmt"
    "log"
    "github.com/csgo/rush_b"
)
func main() {
    //fmt.Println("Hello")
    rush_b.Rand_init()    //初始化种子
    log.SetPrefix("logging:")
    log.SetFlags(0)
    message,err := rush_b.Rush("s1mple")  //传入S1mple
    fmt.Println(message)
    if err !=nil{
      //fmt.Println("it is an err") 
      log.Fatal(err)
    }
}

go run water_man.go
[[email protected] water_man]# go run water_man.go 
s1mple ,follow me rush dust2
[[email protected] water_man]# go run water_man.go 
s1mple ,follow me rush B
[[email protected] water_man]# go run water_man.go 
s1mple ,follow me rush dust2
[[email protected] water_man]# 
4. 让top3给我打佯攻

使用到了map,类似于python中的字典

// rush_b.go
package rush_b

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

func Rand_init(){
   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){   //佯攻
  messages := make(map[string]string)    // map初始化
  
  for _,name := range names{
    message,err := RandomFormat(name)
    if err != 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() {
    //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]])
    }
    
    if err !=nil{
      //fmt.Println("it is an err") 
      log.Fatal(err)
    }
}
go run water_man.go 
[[email protected] water_man]# go run water_man.go 
s2mple ,follow me rush B
zywoo1 ,follow me rush A
device ,follow me rush A

文章有不足的地方欢迎在评论区指出。

欢迎收藏、点赞、提问。关注***饮水机管理员,除了管烧热水,有时还做点别的。