Assume that we have two different packages let's say A and B. I have a struct in package A as following:
type struct1 struct {
X int
Y int
}
In package B, I want to access the variables in struct1 which is package A. How can I do this?
Assume that we have two different packages let's say A and B. I have a struct in package A as following:
type struct1 struct {
X int
Y int
}
In package B, I want to access the variables in struct1 which is package A. How can I do this?
In Go, a type can be exported if it begins with a capital letter. For example, Struct1
is exported and accessible from outside the package, but struct1
is not since it has not been exported. Unexported names are only available inside of that package.
You can import package A into package B and access the exported type as A.Struct1
:
package B
import (
"A"
"fmt"
)
func CreateAndPrintStruct() {
struct1 := A.Struct1{5, 4}
fmt.Printf("%+v", struct1)
}