golang struct中的字段提升

448 阅读1分钟
package main

import (
   "flag"
   "fmt"
   "greet/internal/config"
   "greet/internal/handler"
   "greet/internal/svc"

   "github.com/tal-tech/go-zero/core/conf"
   "github.com/tal-tech/go-zero/rest"
)

var configFile = flag.String("f", "etc/greet-api.yaml", "the config file")

// Structure
type details struct {

   // Fields of the
   // details structure
   name   string
   age    int
   gender string
}

// Nested structure
type student struct {
   branch string
   year   int
   details
}

func main() {

   // Initializing the fields of
   // the student structure
   values := student{
      branch: "CSE",
      year:   2010,
      details: details{
         name:   "Sumit",
         age:    28,
         gender: "Male",
      },
   }


   fmt.Println(values)
   fmt.Println(values.name)
  


}

字段提升后, 嵌入的类型中的字段,就像是直接在本结构体一样,可以直接访问。

www.geeksforgeeks.org/promoted-fi…