在golang中的另一个函数中使用一个函数的变量
using variables of one function in other function in go lang
package main
import (
"encoding/json"
// "flag"
//"fmt"
"strconv"
"html/template"
"time"
// "log"
"strings"
"net/http"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
type microservice_details struct{
Deployment_name string `json:"Deployment_name"`
Namespace string `json:"Namespace"`
Image_tag string
}
type namespace_details struct {
Namespace []string
}
var templates = template.Must(template.ParseGlob("./*.html"))
var label_name string
var namespace string
var microservice ="/microservice/"
var detailed_view="/detailed/"
var kube_config_path="/home/saivamsi/.kube/config"
var config, _ = clientcmd.BuildConfigFromFlags("", kube_config_path)
var clientset, _ = kubernetes.NewForConfig(config)
func main() {
templates = template.Must(template.ParseGlob("./*.html"))
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))
http.Handle("/jpeg/", http.StripPrefix("/jpeg/", http.FileServer(http.Dir("css"))))
http.HandleFunc("/", homepage)
http.HandleFunc(microservice, Deployment)
http.ListenAndServe(":8801",nil)
}
func homepage(w http.ResponseWriter, r *http.Request){
namespace,_:=clientset.CoreV1().Namespaces().List(v1.ListOptions{})
namespace_tmp := namespace_details{}
for _,s := range namespace.Items{
namespace_tmp.Namespace = append(namespace_tmp.Namespace,s.Name)
}
templates.ExecuteTemplate(w,"homepage2.html",namespace_tmp)
}
func Deployment(w http.ResponseWriter, r *http.Request){
name := r.URL.Path[len(microservice):]
var array2 []microservice_details
deploy,_ := clientset.AppsV1().Deployments(name).List(v1.ListOptions{})
microservice_tmp := microservice_details{}
microservice_tmp.Namespace=name
for _, d := range deploy.Items {
microservice_tmp.Deployment_name =d.Name
for _, deployment_containers := range d.Spec.Template.Spec.Containers {
image_tag := deployment_containers.Image[strings.IndexByte(deployment_containers.Image':')+1:]
microservice_tmp.Image_tag=image_tag
}
array2 = append(array2, microservice_tmp)
}
b,_:=json.Marshal(array2)
json.Unmarshal([]byte(b), &array2)
templates.ExecuteTemplate(w ,"microservices2.html",array2)
}
how to use Namespace variable of homepage function in Deployment
function.I need to use them inside go templates.
i have to use that Namespace in microservices.html .i tried many
things but didnt work.
Go is lexically scoped 使用积木。这意味着在函数 ABC()
内声明的任何变量将无法从其他函数 Xyz()
.
访问
如果你需要这样的"sharing",你必须在homepage
全局定义namespace
变量,或者通过引用homepage
传递它,然后deployment
.
package main import ( "encoding/json" // "flag" //"fmt" "strconv" "html/template" "time" // "log" "strings" "net/http" "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" ) type microservice_details struct{ Deployment_name string `json:"Deployment_name"` Namespace string `json:"Namespace"` Image_tag string } type namespace_details struct { Namespace []string } var templates = template.Must(template.ParseGlob("./*.html")) var label_name string var namespace string var microservice ="/microservice/" var detailed_view="/detailed/" var kube_config_path="/home/saivamsi/.kube/config" var config, _ = clientcmd.BuildConfigFromFlags("", kube_config_path) var clientset, _ = kubernetes.NewForConfig(config) func main() { templates = template.Must(template.ParseGlob("./*.html")) http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css")))) http.Handle("/jpeg/", http.StripPrefix("/jpeg/", http.FileServer(http.Dir("css")))) http.HandleFunc("/", homepage) http.HandleFunc(microservice, Deployment) http.ListenAndServe(":8801",nil) } func homepage(w http.ResponseWriter, r *http.Request){ namespace,_:=clientset.CoreV1().Namespaces().List(v1.ListOptions{}) namespace_tmp := namespace_details{} for _,s := range namespace.Items{ namespace_tmp.Namespace = append(namespace_tmp.Namespace,s.Name) } templates.ExecuteTemplate(w,"homepage2.html",namespace_tmp) } func Deployment(w http.ResponseWriter, r *http.Request){ name := r.URL.Path[len(microservice):] var array2 []microservice_details deploy,_ := clientset.AppsV1().Deployments(name).List(v1.ListOptions{}) microservice_tmp := microservice_details{} microservice_tmp.Namespace=name for _, d := range deploy.Items { microservice_tmp.Deployment_name =d.Name for _, deployment_containers := range d.Spec.Template.Spec.Containers {
image_tag := deployment_containers.Image[strings.IndexByte(deployment_containers.Image':')+1:] microservice_tmp.Image_tag=image_tag } array2 = append(array2, microservice_tmp) } b,_:=json.Marshal(array2) json.Unmarshal([]byte(b), &array2)
templates.ExecuteTemplate(w ,"microservices2.html",array2) }
how to use Namespace variable of homepage function in Deployment function.I need to use them inside go templates.
i have to use that Namespace in microservices.html .i tried many things but didnt work.
Go is lexically scoped 使用积木。这意味着在函数 ABC()
内声明的任何变量将无法从其他函数 Xyz()
.
如果你需要这样的"sharing",你必须在homepage
全局定义namespace
变量,或者通过引用homepage
传递它,然后deployment
.