英文:
Writing go programs like node.js apps without go path
问题
在学习Go的基础知识后,我意识到我需要将这个根gocode目录导出为GOPATH。我更希望能够简单地拥有一个项目文件夹,可以在其中安装包并运行我的Go代码,这就是他们编写Go的方式吗?有谁喜欢这样的方式吗?
我已经看过了vendor文件夹选项,但这仍然意味着需要一个gocode的GOPATH导出,并且vendor目录需要在源文件夹结构中。
我希望我的项目可以成为根目录,就这样。为什么Go语言不支持这种方式呢?
或者说,它是支持的吗?
英文:
Upon learning the basics of go I realise I need this root gocode directory to export as GOPATH, I would much rather simply be able to have a project folder, which I can install packages into, and run my go code from within, is this seriously how they've programmed go to be.. who likes this?
I've looked at the vendor folder option but that still means needing a gocode GOPATH export and the vendor directories need to be in that source folder structure.
I want my project to be the root and that's it. How can that not be possible with go lang?
Or is it?
答案1
得分: 1
是的。您需要设置您的GOPATH,所有的Go代码、二进制文件和包等都将放置在这个文件夹中。您可以在$GOPATH/src
的根目录下创建一个文件夹,例如$GOPATH/src/myapp
,然后在这个文件夹中创建一个文件。例如,myapp.go的示例代码如下:
package main
import "fmt"
func main() {
fmt.Print("My app")
}
使用go install $GOPATH/src/myapp/myapp.go
进行编译,并使用$GOPATH/bin/myapp
运行它。
英文:
Yes. You need to set your GOPATH, and all your go-code, -binaries, -packages etc will be placed in this folder. You can make a folder in the root of $GOPATH/src
, e.g $GOPATH/src/myapp
and then create a file within this folder. Example myapp.go
package main
import fmt
func main() {
fmt.Print("My app")
}
Compile it with go install $GOPATH/src/myapp/myapp.go
and run it with $GOPATH/bin/myapp
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论