结构的三种方式

package main

import "fmt"

type Person struct {
   firstName string
   lastName string
}

func main() {
   // 1-struct as a value type: // 值
   var pers1 Person
   pers1.firstName = "Chris"
   pers1.lastName = "Woodward"

   fmt.Printf("The name of the person is %s %s\n",pers1.firstName,pers1.lastName)

   // 2 -struct as a pointer: // 指针
   pers2 := new(Person)
   pers2.firstName = "Chris"
   pers2.lastName = "Woodward"
   (*pers2).lastName = "Woodward" // 这是合法的
   fmt.Printf("The name of person is %s %s\n",pers2.firstName,pers2.lastName)

   // 3--struct as a literal: // 文字
   pers3 := &Person{"Chris","Woodward"}
   fmt.Printf("The name of the person is %s %s\n",pers3.firstName,pers3.lastName)

}

猜你喜欢

转载自blog.csdn.net/q320036715/article/details/84899504