• GO环境及入门案例


    简介

    go不是面向对象语言, 其指针、结构体等比较像C,知名的go 开源项目有docker k8s prometheus node-exporter等

    一、win GO开发环境安装

    这里直接基于IDEA 安装一个GO插件即可
    在这里插入图片描述
    新建项目选择GO,本地如果没有安装GO SDK,可以直接从下面页面选择GO版本,IDEA会自动安装在这里插入图片描述
    GO hello world测试与go命令行测试
    在这里插入图片描述

    二、Linux go运行环境

    curl -s https://mirror.go-repo.io/centos/go-repo.repo | tee /etc/yum.repos.d/go-repo.repo
    yum install go
    
    • 1
    • 2

    IDE 配置原创运行环境
    在这里插入图片描述
    go 编译出来得可执行文件包含运行环境,故包比较大,但运行比较方便。这里编译成.exe文件能在Linux运行也是因为它包含了运行环境,但是这样很奇怪,一般.exe是win程序,故最后还是勾选使用Linux环境进行编译。
    在这里插入图片描述
    运行可能报错编译出来得文件无执行权限,可以chmod +x 增加权限,这个问题可能是IDEA go插件问题,它编译时会编译出两个一样得可执行文件,其中一个没权限,运行时刚好跑了无权限那个。可以考虑专业GO IDEgoland

    二、GO代码入门

    走一遍这个教程也就差不多了解了

    2.1 导包案例

    在这里插入图片描述

    package main
    
    import "fmt"
    import "untitled2/xx"
    
    func main() {
    	fmt.Printf("%s-%d\n", "mystring", 123)
    	println(xx.Add(1, 2))
    	println(xx.Sub(1, 2))
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2.2 赋值

    package main
    
    var a = int8(127)
    // “:=”只能在声明“局部变量”的时候使用,而“var”没有这个限制
    func main() {
    	var b = int8(126)
    	c :=int8(125)
    	println(a,b,c)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.3 变量、函数

    //  类型在变量名后 可省略
    var bb bool = false
    var cc = false
    
    // 返回值类型在入参后,不可省略
    func concatAll(a int, b int, c string) string {
    	all := fmt.Sprintf("%d%d", a, b) + c
    	return all
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.4 三方库使用

    代理等环境变量设置

    go env -w  GO111MODULE=on
    go env -w   GOPROXY=https://mirrors.aliyun.com/goproxy/
    go get k8s.io/client-go@latest
    
    • 1
    • 2
    • 3

    测试拉取第三方库,安装成功后可在go.mod文件看到(go.mod是go的包管理器)

    go get k8s.io/client-go@latest
    
    • 1

    测试官方案例

    /*
    Copyright 2016 The Kubernetes Authors.
    
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
        http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    */
    
    // Note: the example only works with the code within the same release/branch.
    package main
    
    import (
    	"context"
    	"flag"
    	"fmt"
    	"path/filepath"
    	"time"
    
    	"k8s.io/apimachinery/pkg/api/errors"
    	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    	"k8s.io/client-go/kubernetes"
    	"k8s.io/client-go/tools/clientcmd"
    	"k8s.io/client-go/util/homedir"
    	//
    	// Uncomment to load all auth plugins
    	// _ "k8s.io/client-go/plugin/pkg/client/auth"
    	//
    	// Or uncomment to load specific auth plugins
    	// _ "k8s.io/client-go/plugin/pkg/client/auth/azure"
    	// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
    	// _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
    )
    
    func main() {
    	var kubeconfig *string
    	if home := homedir.HomeDir(); home != "" {
    		kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
    	} else {
    		kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
    	}
    	flag.Parse()
    
    	// use the current context in kubeconfig
    	config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
    	if err != nil {
    		panic(err.Error())
    	}
    
    	// create the clientset
    	clientset, err := kubernetes.NewForConfig(config)
    	if err != nil {
    		panic(err.Error())
    	}
    	for {
    		pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
    		if err != nil {
    			panic(err.Error())
    		}
    		fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
    
    		// Examples for error handling:
    		// - Use helper functions like e.g. errors.IsNotFound()
    		// - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message
    		namespace := "default"
    		pod := "example-xxxxx"
    		_, err = clientset.CoreV1().Pods(namespace).Get(context.TODO(), pod, metav1.GetOptions{})
    		if errors.IsNotFound(err) {
    			fmt.Printf("Pod %s in namespace %s not found\n", pod, namespace)
    		} else if statusError, isStatus := err.(*errors.StatusError); isStatus {
    			fmt.Printf("Error getting pod %s in namespace %s: %v\n",
    				pod, namespace, statusError.ErrStatus.Message)
    		} else if err != nil {
    			panic(err.Error())
    		} else {
    			fmt.Printf("Found pod %s in namespace %s\n", pod, namespace)
    		}
    
    		time.Sleep(10 * time.Second)
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
  • 相关阅读:
    JUC系列(七) ForkJion任务拆分与异步回调
    蓝桥杯入门即劝退(六)等差素数数列
    Docker常用命令使用总结
    什么是应变能力?如何提高应变能力?
    华为python面试题目
    openjudge 1.5.24 正常血压
    LeetCode 刷题 [C++] 第279题.完全平方数
    一文1500字从0到1搭建 Jenkins 自动化测试平台
    使用Spring Boot向邮箱发送邮件
    Java基础面试题50题
  • 原文地址:https://blog.csdn.net/qq_39506978/article/details/137435555