适当的项目结构以导入包

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

Proper project structure for importing packages

问题

这里是我为Go应用程序提供的当前结构,它可以提供静态HTTP页面并通过WebSocket发送数据。

在下面的结构中,我有两个包:constants和main,但无法在main包中使用constants。

服务器-客户端游戏
    assets
        css
            index.css
        js
            app.js
        templates
            index.html
    constants
        server.go
        game.go
    main
        main.go
        hub.go
        player.go

我应该使用Go工作区吗?还是可以按照现有的方式工作?

我的项目目前不在GoPath src中,我之前称之为“工作区”。

英文:

As the title here is my current structure for Go app that serves static http page and also sends data through websocket.

In the structure below I have 2 packages constants and main and am unable to use constants from main package.

Server-Client Game
    assets
        css
            index.css
        js
            app.js
        templates
            index.html
    constants
        server.go
        game.go
    main
        main.go
        hub.go
        player.go

Should I be using Go workspace? or can I get it working as is?

My project currently is not in the GoPath src which I was previously calling 'workspace'.

答案1

得分: 3

你应该有一个 GOPATH,其中至少包含 srcbin 两个文件夹。你的项目源代码将放在 src 文件夹中,发布的可执行文件将放在 bin 文件夹中。

例如,如果你的 GOPATH 指向 c:/go-apps,那么你的项目可能看起来像这样:

c:/go-apps/src/Server-Client Game/ ...

如果你执行 go install,你的可执行文件将在 c:/go-apps/bin 中。由于 Go 也支持 Git 仓库,你可以直接导入它们到你的项目中,像这样(假设它是公开的):

import "github.com/gorilla/mux"

这样的语句将从 GitHub 导入流行的 mux 包。你可以在你的 GOPATH 中找到它,路径应该是:

c:/go-apps/src/github.com/gorilla/mux

此外,你可能有多个 GOPATH。当你编译一个应用程序时,它会在你的 GOPATH(s) 中查找包。如果包不可用,它会自动下载它们。

除了使用 go install,你还可以使用 go build 在当前文件夹中构建可执行文件。如果只想下载所有依赖而不进行编译,可以使用 go get

至于文件夹结构,是的,那应该没问题,但我通常将主要代码放在根目录,因为 package mainfunc() main 是入口点,而且在 Go 中每个目录只允许一个包。

英文:

you should have a gopath which should contain at least src and bin. your project source code will go into the src folder and your published binaries will go into your bin folder.

for instance, if your go path points to c:/go-apps then your project may look like this . . .

c:/go-apps/src/Server-Client Game/ ... 

if you issue go install, your binaries will be in c:/go-apps/bin. since go also supports git repositories, you can import them directly into your project like this (assuming it is public)

import "github.com/gorilla/mux"

a statement like this will import the popular mux package from github. you can expect to find this in your gopath to be

c:/go-apps/src/github.com/gorilla/mux

also you may have multiple gopaths. when you compile an application, it will be looking in your gopath(s) for the package. if the package is not available it will automatically download them.

alternatively to go install, you can use go build to build the binaries in the current folder. to simply download all the dependencies without compiling use go get.

as for the folder structure, yes that should be fine, but i usually put my main in the root directory since package main and func() main is the entry point and only one package is allowed per directory in go.

答案2

得分: 1

你需要在主文件中导入你的constants包。你导入自己的包与导入其他非标准包一样,使用在$GOPATH/src目录下的路径。所以如果你的项目位于$GOPATH/src/myproject文件夹中,你可以这样导入你的constants包:

import "myproject/constants"

如果有一个导出的(大写字母开头)常量,比如SomeConstant,你可以在主包中通过constants.SomeConstant来引用它。

英文:

You have to import your constants package in your main files. You import your own packages the same as any other non-standard package, by its path in your $GOPATH/src directory. So if your project sits in the folder $GOPATH/src/myproject , you would import your constants package like so:

import "myproject/constants"

And given an exported (uppercase) constant e.g. SomeConstant, you would refer to it in your main package as constants.SomeConstant.

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

发表评论

匿名网友

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

确定