GoLang中的Getter [关闭]

I have a custom type named ProtectedCustomType and I don't want the variables within that to be set or get directly by the caller, rather want a Getter / Setter methods to do that.

Below is my ProtectedCustomType

package custom
import "fmt"

type ProtectedCustomType struct {
    name string
    age string
    phoneNumber int
}



func (pct *ProtectedCustomType) SetAge (age string)   {
    pct.age=age
    fmt.Println(pct.age)
} 

func (pct *ProtectedCustomType) GetAge ()  string  {
    return pct.age
} 

And here is my main function

package main

import (
    "fmt"
    "./custom"
)

var print =fmt.Println
func structCheck2() {
    pct := custom.ProtectedCustomType{}
    pct.SetAge("23")
    age:=pct.GetAge
    print (age)
}

func main() {
    structCheck2()
}

I am expecting it to print 23, but it is printing as 0x48b950

This (your code) takes the pct instance's GetAge method and stores it in a variable:

age:=pct.GetAge

This calls the GetAge method and stores its return value in a variable:

age:=pct.GetAge()

Consider taking the Tour of Go and reading the Go Specification to get a basic understanding of Go syntax.