Last active
August 29, 2015 13:58
-
-
Save zankich/10274031 to your computer and use it in GitHub Desktop.
Exported/Unexported functions and fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//./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