包准备
首先安装解析yaml文件的包
go get gopkg.in/yaml.v2
但是可能会无法安装成功,报错显示go get unrecognized import path "gopkg.in/yaml.v2"
通过下载编译源代码的方式解决:
再go/src
路径下新建一个文件gopkg.in
,将代码下载到go/src/gopkg.in
下
git config --global http.sslVerify "false"
git config --global http.postBuffer 524288000
go env -w GOPROXY=https://goproxy.cn,direct
git clone https://github.com/go-yaml/yaml.git
或者在https://github.com/go-yaml/yaml上下载zip,解压到该路径下。
之后将yaml的文件名称改为yaml.v2,安装:
cd yaml.v2
go install
main.go
package main
import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
)
//Yaml the yaml file Cluster struct
type Yaml struct {
Clusters Clusters `yaml:"clusters"`
}
type Clusters struct {
Services []Services `yaml:"services"`
}
type Services struct {
Name string `yaml:"service_name"`
Ip string `yaml:"ipaddr"`
Username string `yaml:"username"`
Passwd string `yaml:"passwd"`
Ports []Ports `yaml:"ports,omitempty"`
}
type Ports struct{
Port string