go 基础语法
Last updated on November 22, 2024 pm
🧙 Questions
☄️ Ideas
执行本地脚本
executeCommand := "git clone " + metaRepository
cloneCmd := exec.Command("bash", "-c", executeCommand)
cloneCmd.Stdout = os.Stdout
cloneCmd.Stderr = os.Stderr
cloneCmd.Dir = projectPath + "/" + projectName
err := cloneCmd.Run()
if err != nil {
log.Fatal(err)
os.Exit(1)
} else {
fmt.Println("下载成功")
}
遍历数组
for index, chooseProjectName := range projectList {
fmt.Println("[" + strconv.Itoa(index) + "] " + viper.GetString(chooseProjectName+".name") + ": " + viper.GetString(chooseProjectName+".describe") + " 下载状态 【" + viper.GetString(chooseProjectName+".repository.download") + "】")
}
字符串替换
strings.Replace(metaRepository, "isxcode", viper.GetString("user.account"), -1)
全局变量
var (
projectNumber int
projectPath string
projectName string
)
构建对象
type CloneRepository struct {
Download string `yaml:"download"`
Url string `yaml:"url"`
Name string `yaml:"name"`
}
创建文件夹
_, err := os.Stat(home + "/.isx")
if os.IsNotExist(err) {
err := os.Mkdir(home+"/.isx", 0755)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
调用接口
headers := http.Header{}
headers.Set("Accept", "application/vnd.github+json")
headers.Set("Authorization", "Bearer "+token)
headers.Set("X-GitHub-Api-Version", "2022-11-28")
client := &http.Client{}
req, err := http.NewRequest("GET", "https://api.github.com/octocat", nil)
req.Header = headers
resp, err := client.Do(req)
if err != nil {
fmt.Println("请求失败:", err)
os.Exit(1)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(resp.Body)
// 解析结果
if resp.StatusCode == http.StatusOK {
fmt.Println("登录成功,欢迎使用isx-cli开发工具")
} else {
fmt.Println("无法验证token合法性,登录失败")
os.Exit(0)
}
获取执行时间
startTime := time.Now()
// 打印运行
duration := time.Since(startTime)
fmt.Println("Execution time:", duration)
🔗 Links
go 基础语法
https://ispong.isxcode.com/kubernetes/go/go 基础语法/