在不使用Go路径的情况下编写类似Node.js应用程序的Go程序。

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

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.

huangapple
  • 本文由 发表于 2017年4月28日 17:18:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/43676515.html
匿名

发表评论

匿名网友

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

确定