简体   繁体   中英

How to use struct that is imported from another package

Well, I have my struct Player in package Player

package Player

type Player struct {
    name         string
    speciality   string
}

And I have my main function in package main

package main

import "pack/Player"   

func main() {   
   var player Player.Player
   fmt.Print(player.name)
}

But after I compile it I get

player.name undefined (cannot refer to unexported field or method name)

What I am doing wrong?

You need to export the fields of your structure in order for them to be accessible by having them start with upper case characters:

type Player struct {
    Name         string
    Speciality   string
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM