Leonid Manieiev
Leonid Manieiev

Reputation: 111

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?

Upvotes: 5

Views: 9029

Answers (1)

SirDarius
SirDarius

Reputation: 42879

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
}

Upvotes: 31

Related Questions