douziqian2871 2014-04-21 10:35
浏览 69
已采纳

在OpenShift上的Golang,我找不到本地模块

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条回答 默认 最新

  • duankuiyuant3940 2015-05-16 07:35
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看