I am curious if this type of declaration for structs
t := Person{"girlie", 12}
only works if its type is declared in the same file.
Below are my files.
file st.go, type def structure inside to be used in main func
package structs
type person struct {
age int
name int
}
file practice.go, main function:
package main
import(
"fmt"
"structs/dir"
)
func main() {
var s dir.Person
s.Name = "She"
s.Age = 12
>> t := Person{"girlie", 12}
fmt.Println(s.Name)
fmt.Println(t.Name)
}
As you can see an error occurs where instance t is declared.
You're missing the package reference, it should be:
t := dir.Person{"girlie", 12}
// ^^^^ missing this part
Assuming that the line var s dir.Person
works, which I'm guessing it does based on the question, which means that your quoted contents of "st.go" are not accurate, because the package name is different, and the struct and its fields are not exported.