无法在golang中创建自定义包。

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

Not able to create custom packages in golang

问题

我对GO还不太熟悉。我正在尝试一些例子,其中一个是使用cgo从GO调用C代码。

这是我正在尝试的例子:https://code.google.com/p/go-wiki/wiki/cgo

参考上面的链接,我创建了以下的包结构:

gocode/src/github.com/mypkg/test.go

"mypkg"是我创建的自定义包,并在test.go中使用了它:

package mypkg

当我运行我的go程序时,出现了一个错误。
"go run: cannot run non-main package"

我已将GOPATH设置为GO源代码文件夹。

GOPATH=/xyz/gocode/src/

我搜索了解决方案,并找到了以下链接,说自定义包无法创建:
https://groups.google.com/forum/#!topic/golang-nuts/vmebkoqYMH4

http://stackoverflow.com/questions/23870801/go-run-cannot-run-non-main-package

但是,我看到的所有代码都有一个自定义包名。请帮助我解决这个问题。

非常感谢您的帮助。

英文:

I am pretty new to GO. I was trying few examples and one of them was to use cgo to call a C code from GO.

Here is the example I am trying: https://code.google.com/p/go-wiki/wiki/cgo

With the above link as a reference I created a package structure as below:

gocode/src/github.com/mypkg/test.go

"mypkg" is the custom package that I have created and used it in the test.go as below:

package mypkg

I get an error when I run my go program.
"go run: cannot run non-main package"

I have set my GOPATH to the GO source code folder.

GOPATH=/xyz/gocode/src/

I searched for solutions and found the below links which says, custom pacakages cannot be created:
https://groups.google.com/forum/#!topic/golang-nuts/vmebkoqYMH4

http://stackoverflow.com/questions/23870801/go-run-cannot-run-non-main-package

But, All the code I see is with a custom pacakage name. Please help me to resolve this issue.

Any help is really appreciated.

Thanks

答案1

得分: 3

$GOPATH中创建mypackagemypackage.go目录。

mypackage.go中放入以下代码:

package mypackage

func Hello() {
   println("hello")
}

创建main.go文件:

package main

import "mypackage"

func main() {
     mypackage.Hello()
}

应该将go run命令应用于main包。

在你的情况下,创建main.go文件并导入github.com/mypkg

此外,导入的包不应包含main函数。

英文:

Create dir mypackage and mypackage.go in $GOPATH

put in mypackage.go following code

package mypackage

func Hello() {
   println("hello")
}

Create main.go

package main

import "mypackage"

func main() {
     mypackage.Hello()
}

go run command must be applied to main package

In you case, create main.go file and import github.com/mypkg

Also importing packages should not contain main function.

答案2

得分: 2

go run 用于执行可执行文件,适用于单独的文件。使用 go installgo build 来处理包。更多阅读:http://golang.org/doc/code.html。

英文:

go run is for executables, and works on separate files. Use go install and go build to work with packages. Additional reading: http://golang.org/doc/code.html.

答案3

得分: 0

我已经遇到过这个问题。它显示这个错误是因为任何.go文件中都没有主包。你应该创建一个主文件夹,在其中创建一个main.go文件来调用主包函数main,并在其中导入你的包,就像下面这样:

package main
import(
    "fmt"
    "github.com/mypkg"
)
func main(){
// 在这里使用你的包函数,使用 packagename.functionname
    mypkg.functionName()
    fmt.Println("Hello")
}

现在,通过进入mypkg目录并在终端或命令行中运行go build来构建包,如果你是Windows用户。返回到包含main.go文件的主目录,并运行go install,它将在主目录中创建可执行文件。目录结构如下:

workspace
  ├── bin
  │   └── app
  ├── pkg
  │   └── linux_amd64
  |         └── user
  |              └── handlers.a
  └── src
    ├── bitbucket.org
    ├── github.com
         └── user
              └── app
                   ├── main.go
                   └── mypkg
                         └──mypkg.go
英文:

I have been through this problem. It is showing this error because there is no main package in any .go file. You should create a main folder in which create a main.go file to call main package function main and import your package in that just like below:

package main 
import(
    "fmt"
    "github.com/mypkg"
)
func main(){
// use use your package functions here by packagename.functionname
    mypkg.functionName()
    fmt.Println("Hello")
}

Now build the package by moving to mypkg directory and run go build on terminal or command line if you are window user.
Move up to main directory which contains main.go file and run go install
it will create executable file in the name of main directory. The directory structure be like.

workspace
  ├── bin
  │   └── app
  ├── pkg
  │   └── linux_amd64
  |         └── user
  |              └── handlers.a
  └── src
    ├── bitbucket.org
    ├── github.com
         └── user
              └── app
                   ├── main.go
                   └── mypkg
                         └──mypkg.go

huangapple
  • 本文由 发表于 2015年7月7日 00:39:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/31251204.html
匿名

发表评论

匿名网友

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

确定