How to access exported members of a unexported struct


This is an example that access exported members from a unexported struct.

// pk/pk.go
package pk
type point struct {
    X, Y int
}

func NewPoint(x, y int) *point { // return a pointer, the struct is hidden
     return &point{x, y}
}


// main.go
package main
import "fmt"
func main() {
     p := pk.NewPoint(2, 3)
     fmt.Println(p.X, p.Y)
}



  


Comments:

Write a comment
Anonymous

Captcha image

Reload

Type the number you see in the image above: