在OpenShift上使用Golang时,我找不到本地模块。

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

Golang on OpenShift, I can't find local module

问题

我想为我的Golang应用程序使用一个OpenShift测试环境。

我创建了一个测试应用程序:

myproj/
------web.go
------/mylib/
-------------mylib.go

web.go是标准的OpenShift文件:

package main

import (
    "fmt"
    "net/http"
    "os"
    "runtime"
    "./mylib"
)

func main() {
    http.HandleFunc("/", hello)
    bind := fmt.Sprintf("%s:%s", os.Getenv("HOST"), os.Getenv("PORT"))
    fmt.Printf("listening on %s...", bind)
    err := http.ListenAndServe(bind, nil)
    if err != nil {
	    panic(err)
    }
}

func hello(res http.ResponseWriter, req *http.Request) {
    str := mylib.Lib();
    fmt.Fprintf(res, "hello, %s from %s", str, runtime.Version())
}

我创建了"mylib":

package mylib

func Lib() string {
    return "world"
}

当我在本地计算机上运行"go run web.go"时,一切都正常。但是当我尝试将这段代码上传到OpenShift时,我收到以下错误:

remote: -----> Using Go 1.1.2
remote: -----> Running: go get -tags openshift ./...
remote: can't load package: /var/lib/openshift/5354e6fd4382ec2dca000223/app-root/runtime/repo/.openshift/g/src/github.com/smarterclayton/goexample/web.go:8:2: local import "./mylib" in non-local package
remote: An error occurred executing 'gear postreceive' (exit code: 1)
remote: Error message: CLIENT_ERROR: Failed to execute: 'control build' for /var/lib/openshift/5354e6fd4382ec2dca000223/go

这是什么意思?为什么Golang找不到这个包?我不能把所有的代码都写在一个文件中。我应该如何为OpenShift编写应用程序?

英文:

I want to use an OpenShift test environment for my Golang applications.

I made a test application:

myproj/
------web.go
------/mylib/
-------------mylib.go

web.go is standard OpenShift file:

package main

import (
    "fmt"
    "net/http"
    "os"
    "runtime"
    "./mylib"
)

func main() {
    http.HandleFunc("/", hello)
    bind := fmt.Sprintf("%s:%s", os.Getenv("HOST"), os.Getenv("PORT"))
    fmt.Printf("listening on %s...", bind)
    err := http.ListenAndServe(bind, nil)
    if err != nil {
	    panic(err)
    }
}

func hello(res http.ResponseWriter, req *http.Request) {
    str := mylib.Lib();
    fmt.Fprintf(res, "hello, %s from %s", str, runtime.Version())
}

and I created "mylib"

package mylib

func Lib() string {
    return "world"
}

and when I run "go run web.go" everything works fine on my local computer. But when I try to upload this code to OpenShift I get the following error:

remote: -----> Using Go 1.1.2
remote: -----> Running: go get -tags openshift ./...
remote: can't load package: /var/lib/openshift/5354e6fd4382ec2dca000223/app-root/runtime/repo/.openshift/g/src/github.com/smarterclayton/goexample/web.go:8:2: local import "./mylib" in non-local package
remote: An error occurred executing 'gear postreceive' (exit code: 1)
remote: Error message: CLIENT_ERROR: Failed to execute: 'control build' for /var/lib/openshift/5354e6fd4382ec2dca000223/go

What does this mean? Why can't Golang find this package? I can't write all code in one file. How should I write the application for OpenShift?

答案1

得分: 2

我知道这个问题很旧,但我遇到了同样的问题,很难找到解决办法,所以我决定提问,以帮助那些遇到同样问题的人。

解决方案非常简单,可以在 GitHub 上的 go cartdrige 仓库的 readme 中找到:github.com/smarterclayton/openshift-go-cart

你需要创建一个名为 .godir 的文件,并在其中放置你的服务器主包的名称。
例如,如果你放置了 myserver,你可以使用以下代码:

package main
import "myserver/mylib"
func main() {
    mylib.DoStuff()
}

基本上,在推送到 openshift 之前,仓库会被复制到 .godir 所指定的目录中进行构建。

英文:

I know this question is old but i had the same problem and it was difficult to find the solution, so i decided to ask in order to help who will run on the same problem.

The solution is very simple and can be found in readme of the go cartdrige repo on github: github.com/smarterclayton/openshift-go-cart

You have to create a file named .godir and put here the name of the main package of your server.
For example if you put myserver you can use:

package main
import "myserver/mylib"
func main() {
    mylib.DoStuff()
}

Basically when you push on openshift the repo is copied in the directory placed in .godir before the build

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

发表评论

匿名网友

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

确定