英文:
How to use one class in another class that is in the same package?
问题
我对Go语言还比较新,正在制作一个小型的单词卡片应用程序。
我的包结构如下:
VocabHelper
|
-|src
--|com
---|wks
----|card
------Card.go
------Deck.go
----|main
------main.go
Deck和Card是两个独立的类,但它们在同一个包中:
Card.go
package card
type Card struct{
Question string
Answer string
}
Deck.go
package card
import (
"math/rand"
)
type Deck struct{
Cards []card.Card
}
当我尝试编译项目时,编译器报错说undefined:card
,尽管card和deck在同一个包中。我该如何在deck类中使用card类?
英文:
I'm fairly new to Go and I'm making an a small flashcards app.
My package structure is this
VocabHelper
|
-|src
--|com
---|wks
----|card
------Card.go
------Deck.go
----|main
------main.go
Deck and Card are two separate classes but they're in the same package:
Card.go
package card
type Card struct{
Question string
Answer string
}
Deck.go
package card
import (
"math/rand"
)
type Deck struct{
Cards []card.Card
}
When I try to compile the project, the compiler says undefined:card
even though card is in the same package as deck. How can I use the card class in the deck class?
答案1
得分: 5
去掉card.
部分,它应该可以工作。它们都在同一个包中。
type Deck struct{
Cards []Card
}
英文:
Leave off the card.
part, and it should work. They're both in the same package.
type Deck struct{
Cards []Card
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论