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?

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top