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 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献