Given the following three go packages (I abbreviated the import paths for imp and Tdef for privacy reasons)
package main
import (
"imp"
"fmt"
)
func main() {
T := imp.NewT()
fmt.Printf("T.X = %d
", T.X)
fmt.Printf("T has type %T
", T)
}
package imp
import (
"Tdef"
)
func NewT() Tdef.T {
return Tdef.T{0,0}
}
package Tdef
type T struct {
X int
Y int
}
func (T T) GetX() int {
return T.X
}
main() produces the output
T.X = 0
T has type Tdef.T
This means that I can use a variable of type T in a package where the definition of T is not visible by defining it using a return value, and I can use its exported fields (and methods, not shown here).
I find that a bit surprising and did not find any information on this in the spec. Then again, I don't have much programming experience -- is this to be expected, and is it bad practice to make use of it (I don't have a real-life example right now, mind you)?