亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 go lang 的另一個函數中使用一個函數的變量

在 go lang 的另一個函數中使用一個函數的變量

Go
墨色風雨 2022-05-18 09:41:18
   package mainimport (   "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 stringvar namespace stringvar 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)}如何在部署函數中使用主頁函數的命名空間變量。我需要在 go 模板中使用它們。我必須在 microservices.html 中使用該命名空間。我嘗試了很多東西但沒有奏效。
查看完整描述

1 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

Go使用塊在詞法范圍內。這意味著在函數內聲明的任何變量ABC()都不能從其他函數訪問Xyz()。

如果你需要這樣的“共享”,你必須namespace在全局定義變量homepage,或者通過引用傳遞homepage,然后deployment


查看完整回答
反對 回復 2022-05-18
  • 1 回答
  • 0 關注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號