在GO中使用多个文件构建(GO1)

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

Building with multiple files in GO (GO1)

问题

有人知道是否可以使用"go build"命令从一个目录中的多个文件构建吗?例如:

src/
  file1.go
  file2.go

其中file1.go包含"func main()"方法,而file2提供支持函数。我尝试使用以下导入语句,但没有任何进展:

import (
  "file2"
)

import (
  file2 "./file2"
)

我想知道是否需要修改GOROOT环境变量才能使其工作。或者我只是愚蠢地忽略了一些明显的东西。

谢谢

英文:

Does anyone know if it's possible to use the "go build" command to build from multiple files in a directory? For example

src/
  file1.go
  file2.go

Where file1.go contains the "func main()" method and file2 provides supporting functions. I've tried by using the following import statements but I'm getting no where

import (
  "file2"
)

import (
  file2 "./file2"
)

I'm wondering if this is a case that I need to modify the GOROOT environment variable to get this to work. Or if I'm just being daft and missing something blindingly obvious.

Thanks

答案1

得分: 5

如果file1.go和file2.go属于同一个包,这应该可以正常工作。你不需要将同一个包中的文件导入到彼此中。它们的变量和函数已经是共享的。

如果这些文件属于不同的包,它们应该在不同的目录中。

英文:

If file1.go and file2.go are part of the same package, this should work fine. You don't have to import files from the same package into each other. Their variables and functions are already shared.

If the files belong to different packages, they should be in different directories.

huangapple
  • 本文由 发表于 2012年3月30日 17:11:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/9940073.html
匿名

发表评论

匿名网友

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

确定