Search code examples
structgoimportundefined

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?


Solution

  • 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
    }