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
Related
I'm trying to call another lambda function using the following code:
sess := session.Must(
session.NewSessionWithOptions(
session.Options{
SharedConfigState: session.SharedConfigEnable,
},
),
)
svc := lambda.New(sess, &aws.Config{Region: aws.String("ap-east-1")})
result, err := svc.Invoke(&lambda.InvokeInput{
FunctionName: aws.String(os.Getenv("testLambdaFunc")),
Payload: []byte(req.Body),
})
But there are two errors
New not declared by package lambda
and
InvokeInput not declared by package lambda
I've tried to initialize the go.mod file, but it doesn't fix both of the errors.
Any ideas?
The imports of my main.go file:
"fmt"
"os"
"pkg/log"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
And the go.mod file
module somefunc
go 1.16
require (
github.com/aws/aws-lambda-go v1.27.0
github.com/aws/aws-sdk-go v1.40.59
pkg/log v0.0.0-00010101000000-000000000000
)
replace pkg/log => ./../../go/common/pkg/log
Based on your code snipped, it looks as if you are using the AWS SDK for Go V2. It is recommended to use the AWS SDK for Go V2 (please review the Migrating to the AWS SDK for Go V2 documentation).
Please initialize a Go Modules project (as described on the SDK's Github page):
mkdir YOUR-PROJECT
cd YOUR-PROJECT
go mod init YOUR-PROJECT
Add the dependencies as follows:
go get github.com/aws/aws-sdk-go-v2/aws
go get github.com/aws/aws-sdk-go-v2/config
go get github.com/aws/aws-sdk-go-v2/service/lambda
The above should give you a working project where the dependencies for the SDK packages will resolve.
The corresponding V2 code will look something like the following:
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion("us-east-1"),
)
if err != nil {
[...]
}
svc := lambda.NewFromConfig(cfg)
result, err := svc.Invoke(context.TODO(),
&lambda.InvokeInput{
FunctionName: aws.String(os.Getenv("testLambdaFunc")),
Payload: []byte(req.Body),
},
)
Trying to build a simple crud api with Golang and Echo and I can't get past the first tep in the Echo docs.
I run go get -u github.com/labstack/echo/...
then I create server.go:
package main
import (
"net/http"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":1323"))
}
but when I try to run: go run server.go
I get this error:
server.go:6:2: cannot find package "github.com/labstack/echo/v4" in any of:
/usr/local/Cellar/go/1.14.4/libexec/src/github.com/labstack/echo/v4 (from $GOROOT)
/Users/dariusgoore/go/src/github.com/labstack/echo/v4 (from $GOPATH)
You need to enable GO111MODULE. To enable the module you need to run this command.
export GO111MODULE=on
After enabling it when you will run go run server.go then it will install the packages again after that the program will run as expected.
I get the same issue when I run go get before go mod init. Using the following commands, I can run the server successfully:
go mod init example.com/try-echo
go get
go run server.go
I just created a new project with the same main.go and it ran without any issue. I have listed the steps I followed.
package main
import (
"net/http"
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":1323"))
}
and used go.mod for dependencies downloads
go mod init
go get
go run main.go
and it ran without any error
____ __
/ __/___/ / ___
/ _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.1.16
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
O\
⇨ http server started on [::]:1323
I hope this helps. If it doesn't you can run go env and share the result of the command, which will help to debug further.
export GO111MODULE=on
go mod init
go mod tidy
if not succed, add more command:
go mod download
I'm writing some code that needs access to a Cobra CLI object from another GitHub repo:
package main
import (
"github.com/spf13/cobra/doc"
"github.com/sylabs/singularity/cmd/singularity/cli"
"log"
)
func main() {
err := doc.GenReSTTree(cli.SingularityCmd, "./")
if err != nil {
log.Fatal(err)
}
}
I also have the following version constraint:
[[constraint]]
name = "github.com/sylabs/singularity"
version = "3.0.3"
Now, when I go install, I get the error:
# github.com/TMiguelT/singularity-userdocs/vendor/github.com/sylabs/singularity/internal/pkg/runtime/engines/config/starter
vendor/github.com/sylabs/singularity/internal/pkg/runtime/engines/config/starter/starter.go:10:10: fatal error: starter.h: No such file or directory
#include "starter.h"
^~~~~~~~~~~
compilation terminated.
So when I try to build my code, it tries to compile the singularity module and fails because it can't find some C code. This header file is located here in the repo I'm importing: https://github.com/sylabs/singularity/blob/v3.0.3/cmd/starter/c/starter.h
How can I make my go install aware of this, to ensure my project compiles?
I have a main.go file that uses the proto files in pkg/models to Marshal and Unmarshal a proto struct like this:
// Convert to string
protoStr := proto.MarshalTextString(proto)
// Unmarshal string back to proto struct
var proto2 models.Stuff
err := proto.UnmarshalText(protoStr, &proto2)
The setup is here: https://github.com/chuyval/qqs/tree/master/q2
The project contains a vendor directory that only has the github.com/golang/protobuf repo checked out. (run glide install to create vendor if it doesnt exist)
The main.go program works fine when running go run main.go from inside the project.
When I move the main.go file one level up to the parent directory, and run the same command go run main.go at the parent level, it reports the following error:
line 2: unknown field name "value_list" in models.Stuff
When I delete the vendor directory in the project directory, and run go run main.go at the parent level, I get no error.
Why would having a vendor directory in the project repository make it error out?
Another thing to note, if I run the same main.go application inside of the dependent repo, it works every time (with or without the vendor repository).
Sample code:
package main
import (
"github.com/chuyval/qqs/q2/pkg/models"
"github.com/golang/protobuf/proto"
"log"
)
func main () {
stuff := createProtoStuff()
log.Printf("Stuff: %+v", stuff)
// Convert to string
stuffStr := proto.MarshalTextString(stuff)
// Unmarshal string back to proto struct
var stuff2 models.Stuff
err := proto.UnmarshalText(stuffStr, &stuff2)
if err != nil {
log.Printf("It didnt work. Error: %s", err.Error())
} else {
log.Printf("It worked. Proto: %+v", stuff2)
}
}
func createProtoStuff() *models.Stuff {
someValueList := []*models.SomeValue{&models.SomeValue{Id: "Some Value ID"}}
valueList := &models.SomeValueList{SomeValue: someValueList}
stuffValueList := &models.Stuff_ValueList{
ValueList: valueList,
}
stuff := &models.Stuff{
Id: "Stuff List Id",
Stuff: stuffValueList,
}
return stuff
}
Software Versions
glide version 0.13.1
go version go1.10.3 darwin/amd64
protoc version libprotoc 3.6.0
I am working on testing an application of mine, for which I need to create temporary files with specific extensions. My goal is to create files in a temp directory that look similar to this example123.ac.json.
In order to do this I am using ioutil.TempDir and ioutil.TempFile.
Here is a small contrived example of what I am doing.
main.go:
package main
func main() {
}
main_test.go:
package main
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
func TestMain(t *testing.T) {
dir, err := ioutil.TempDir("", "testing")
if err != nil {
t.Fatalf("unable to create temp directory for testing")
}
defer os.RemoveAll(dir)
file, err := ioutil.TempFile(dir, "*.ac.json") // Create a temporary file with '.ac.json' extension
if err != nil {
t.Fatalf("unable to create temporary file for testing")
}
fmt.Printf("created the following file: %v\n", file.Name())
}
When I run the tests locally on my Mac with go test the following is outputted from the fmt.Printf is
$ go test
created the following file: /var/folders/tj/1_mxwn350_d2c5r9b_2zgy7m0000gn/T/testing566832606/900756901.ac.json
PASS
ok github.com/JonathonGore/travisci-bug 0.004s
So it works as expected but when I run it in TravisCI the following is outputted from the Printf statement:
created the following file: /tmp/testing768620677/*.ac.json193187872
For some reason it is using the literal asterisk inside TravisCI but not when running on my own computer.
Here is a link to the TravisCI logs if interested.
For completeness here is my .travis.yml:
language: go
go:
- "1.10"
Anyone have any idea what is going on here? Or am I missing something obvious?
The feature of replacing the first asterisk with the random value was added in Go 1.11. It looks like you are using go 1.10 for your Travis CI runs so the asterisk won't be replaced.