无法在Golang中包含多个文件。

huangapple go评论83阅读模式
英文:

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 结构体中的 NumberOfDoorsCylinders 字段的首字母改为大写,以便从 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 and Cylinders fields from car.Car struct, so that they can be accessed from main package
  • replace the call to Car{4,6} with car.Car{4,6}
  • code in car.go is not part of package main anymore, you can view it as a car library now. That's why the Car struct is now declared being part of package car

Like that, you can build and run in one step with:

go run main.go

huangapple
  • 本文由 发表于 2016年1月10日 00:03:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/34695689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定