Skip to content

Instantly share code, notes, and snippets.

@zankich
Last active August 29, 2015 13:58
Show Gist options
  • Save zankich/10274031 to your computer and use it in GitHub Desktop.
Save zankich/10274031 to your computer and use it in GitHub Desktop.
Exported/Unexported functions and fields
Your project layout should be
./main.go
./myProject/myProject.go
then run the program with
$ go run main.go
If you comment out lines 11 and 12 of main.go, then the program will execute correctly
package main
import "fmt"
import "./myProject"
func main() {
s := myProject.NewPerson()
fmt.Println(s.Name)
fmt.Println(s.DOB)
fmt.Println(s.GetSSN()) // This line will return the proper results
fmt.Println(s.superSecretFunction()) // This line will error out
fmt.Println(s.ssn) // so will this one
}
//./myProject/myProject.go
package myProject
type Person struct {
Name string
DOB string
ssn string
}
func (p Person) GetSSN() string {
return p.superSecretFunction()
}
func (p Person) superSecretFunction() string {
return p.ssn
}
func NewPerson() Person {
s := Person{}
s.Name = "Adrian Zankich"
s.DOB = "11/05/1955"
s.ssn = "xxx-xx-xxxx"
return s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment