英文:
Unable to include multi file in the golang
问题
我知道这个问题之前已经被问过了,我尝试了所有可能的答案,但是仍然没有帮助到我。
然而,为了再次澄清问题并详细说明我的问题。我实际上正在尝试将一个简单的文件包含到main.go文件中。我的文件夹结构和其他信息如下:
\src\
Multi-file\
lib\Car.go
main.go
Car.go
package main
type Car struct {
numberOfDoors int
cylinders int
}
main.go
package main
import (
"fmt"
)
func main() {
c := Car{4,6}
fmt.Println(c)
}
当我编译main.go时,我得到以下错误
# command-line-arguments
.\main.go:8: undefined: Car
这是我的go env详细信息:
set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\xampp\htdocs\golang
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GO15VENDOREXPERIMENT=
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
FYI:我尝试过将其包含为包,但这甚至没有帮助到我。
非常感谢任何帮助。
英文:
I know this question has been asked before and I've tried all possible answers, but nothing still didn't help me.
However to refresh the question again and to elaborate my problem. I'm actually trying to include a simple file to the main.go file. My folder structure and the rest of the information as follows:
\src\
Multi-file\
lib\Car.go
main.go
Car.go
package main
type Car struct {
numberOfDoors int
cylinders int
}
main.go
package main
import (
"fmt"
)
func main() {
c := Car{4,6}
fmt.Println(c)
}
When I complie the main.go I'm getting the following error
# command-line-arguments
.\main.go:8: undefined: Car
Here is my go env details:
set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\xampp\htdocs\golang
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GO15VENDOREXPERIMENT=
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
FYI: I tried including as package also that didn't even help me out.
Any help would be extremely appreciated.
答案1
得分: 5
请按照以下说明设置您的工作空间以进行程序和库的操作:如何编写Go代码。
对于您的示例,一个包含两个文件的main
包:
src/car
├── car.go
└── main.go
$ cd src/car
$ go run main.go car.go
{4 6}
$ go build car && ./car
{4 6}
$ go install car && car
{4 6}
$
main.go
:
package main
import (
"fmt"
)
func main() {
c := Car{4, 6}
fmt.Println(c)
}
car.go
:
package main
type Car struct {
numberOfDoors int
cylinders int
}
英文:
Set up your workspace for programs and libraries by following the instructions: How to Write Go Code.
For your example, a two file main
package,
src/car
├── car.go
└── main.go
$ cd src/car
$ go run main.go car.go
{4 6}
$ go build car && ./car
{4 6}
$ go install car && car
{4 6}
$
main.go
:
package main
import (
"fmt"
)
func main() {
c := Car{4, 6}
fmt.Println(c)
}
car.go
:
package main
type Car struct {
numberOfDoors int
cylinders int
}
答案2
得分: 1
你不应该有两个文件,它们声明在同一个包中,但不在同一个目录中。一个解决方案是使用以下目录结构:
src\
car\
car.go
main.go
car.go
package car
type Car struct {
NumberOfDoors int
Cylinders int
}
main.go
package main
import (
"car"
"fmt"
)
func main() {
c := car.Car{4, 6}
fmt.Println(c)
}
当然,Car
结构体不再属于 main
包,所以我必须做以下更改:
- 将
car.Car
结构体中的NumberOfDoors
和Cylinders
字段的首字母改为大写,以便从main
包中访问它们。 - 将对
Car{4,6}
的调用替换为car.Car{4,6}
。 car.go
中的代码不再属于main
包,现在可以将其视为一个car
库。这就是为什么现在将Car
结构体声明为package car
的一部分。
这样,你可以使用以下命令一步构建和运行:
go run main.go
英文:
You should not have 2 files, that are declared in the same package but are not residing in the same directory. One solution could be to have this directory structure instead:
src\
car\
car.go
main.go
car.go
package car
type Car struct {
NumberOfDoors int
Cylinders int
}
main.go
package main
import (
"car"
"fmt"
)
func main() {
c := car.Car{4, 6}
fmt.Println(c)
}
Of course, Car
structure not being in main
package, I had to:
- change to uppercase the first letter of
NumberOfDoors
andCylinders
fields fromcar.Car
struct, so that they can be accessed frommain
package - replace the call to
Car{4,6}
withcar.Car{4,6}
- code in
car.go
is not part of packagemain
anymore, you can view it as acar
library now. That's why theCar
struct is now declared being part ofpackage car
Like that, you can build and run in one step with:
go run main.go
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论