I have a golang structure something like this:
type MyStruct struct {
Id string
}
and function:
func (m *MyStruct) id() {
// doing something with id here
}
Also I have another structure like this:
type MyStruct2 struct {
m *MyStruct
}
Now I have a function:
func foo(str *MyStruct2) {
str.m.id()
}
But I'm getting error in compile time:
str.m.id undefined (cannot refer to unexported field or method mypackage.(*MyStruct)."".id
How can I call this function correctly?
Thank you
From http://golang.org/ref/spec#Exported_identifiers:
So basically only functions / variables starting with a capital letter would be usable outside the package.
Example:
If you change
MyStruct.Id
toMyStruct.id
, you'll no longer be able to access it to initializeMyStruct2
, because,id
will be accessible only through its own package (which isfirst
package).This is because
MyStruct
andMyStruct2
are in different packages.To solve that you can do this:
Package
first
:Package
second
: