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?
Related
I am attempting to create named loggers automatically for HTTP handlers that I'm writing, where I am passed a function (pointer).
I'm using the code mentioned in this question to get the name of a function:
package utils
import (
"reflect"
"runtime"
)
func GetFunctionName(fn interface{}) string {
value := reflect.ValueOf(fn)
ptr := value.Pointer()
ffp := runtime.FuncForPC(ptr)
return ffp.Name()
}
I'm using this in my main function to try it out like so:
package main
import (
"github.com/naftulikay/golang-webapp/experiments/functionname/long"
"github.com/naftulikay/golang-webapp/experiments/functionname/long/nested/path"
"github.com/naftulikay/golang-webapp/experiments/functionname/utils"
"log"
)
type Empty struct{}
func main() {
a := long.HandlerA
b := path.HandlerB
c := path.HandlerC
log.Printf("long.HandlerA: %s", utils.GetFunctionName(a))
log.Printf("long.nested.path.HandlerB: %s", utils.GetFunctionName(b))
log.Printf("long.nested.path.HandlerC: %s", utils.GetFunctionName(c))
}
I see output like this:
github.com/naftulikay/golang-webapp/experiments/functionname/long.HandlerA
This is okay but I'd like an output such as long.HandlerA, long.nested.path.HandlerB, etc.
If I could get the Go module name (github.com/naftulikay/golang-webapp/experiments/functionname), I can then use strings.Replace to remove the module name to arrive at long/nested/path.HandlerB, then strings.Replace to replace / with . to finally get to my desired value, which is long.nested.path.HandlerB.
The first question is: can I do better than runtime.FuncForPC(reflect.ValueOf(fn).Pointer()) for getting the qualified path to a function?
If the answer is no, is there a way to get the current Go module name using runtime or reflect so that I can transform the output of runtime.FuncForPC into what I need?
Once again, I'm getting values like:
github.com/naftulikay/golang-webapp/experiments/functionname/long.HandlerA
github.com/naftulikay/golang-webapp/experiments/functionname/long/nested/path.HandlerB
github.com/naftulikay/golang-webapp/experiments/functionname/long/nested/path.HandlerC
And I'd like to get values like:
long.HandlerA
long.nested.path.HandlerB
long.nested.path.HandlerC
EDIT: It appears that Go does not have a runtime representation of modules, and that's okay, if I can do it at compile time that would be fine too. I've seen the codegen documentation and I'm having a hard time figuring out how to write my own custom codegen that can be used from go generate.
The module info is included in the executable binary, and can be acquired using the debug.ReadBuildInfo() function (the only requirement is that the executable must be built using module support, but this is the default in the current version, and likely the only in future versions).
BuildInfo.Path is the current module's path.
Let's say you have the following go.mod file:
module example.com/foo
Example reading the build info:
bi, ok := debug.ReadBuildInfo()
if !ok {
log.Printf("Failed to read build info")
return
}
fmt.Println(bi.Main.Path)
// or
fmt.Println(bi.Path)
This will output (try it on the Go Playground):
example.com/foo
example.com/foo
See related: Golang - How to display modules version from inside of code
If your goal is to just have the name of the module available in your program, and if you are okay with setting this value at link time, then you may use the -ldflags build option.
You can get the name of the module with go list -m from within the module directory.
You can place everything in a Makefile or in a shell script:
MOD_NAME=$(go list -m)
go build -ldflags="-X 'main.MODNAME=$MOD_NAME'" -o main ./...
With main.go looking like:
package main
import "fmt"
var MODNAME string
func main() {
fmt.Println(MODNAME) // example.com
}
With the mentioned "golang.org/x/mod/modfile" package, an example might look like:
package main
import (
"fmt"
"golang.org/x/mod/modfile"
_ "embed"
)
//go:embed go.mod
var gomod []byte
func main() {
f, err := modfile.Parse("go.mod", gomod, nil)
if err != nil {
panic(err)
}
fmt.Println(f.Module.Mod.Path) // example.com
}
However embedding the entire go.mod file in your use case seems overkill. Of course you could also open the file at runtime, but that means you have to deploy go.mod along with your executable. Setting the module name with -ldflags is more straightforward IMO.
I have the following code written in package test/api under project A:
func ListClusterTestCase() {
...
getResponse, err = ocm.Connection.Get().
Path(ClustersEndpoint).
Parameter("order", "creation_timestamp desc").
Send()
getResult := ReadResponse(getResponse, err, 200)
clusters := GetClusters(getResult)
Expect(findCluster(clusters, clusterID)).To(BeTrue()) // <-- private function defined in a different file in test/api (called clusters_test.go)
}
...
func ProbeTests() []*ocm.TestCase {
tc := []*ocm.TestCase{}
...
tc = append(tc, ListClustersTestCase(cfg)...)
return tc
}
And a file in a different project - project B - which imports this test/api package is trying to reference ProbeTests:
import (
cms "my/project/test/api"
)
func AddTests(cfg *ocm.TestConfig) {
...
ocm.AddTestCases(cms.ProbeTests(cfg))
}
I am getting the following compilation error when trying to compile project B:
../../go/pkg/mod/.../test/api/clusters_load_test_cases.go:59:12: undefined: findCluster
Why cant my project compile? why doesn't it compile the entire package test/api? does it only compile the file containing the exported ListClusterTestCase? when I compile project A it works just fine.
You need to move the findCluster declaration to a non-test file if you don't want it to be omitted when the package is imported.
When compiling packages, build ignores files that end in '_test.go'.
https://golang.org/cmd/go/#hdr-Compile_packages_and_dependencies
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 trying to use Go's plugin system. Even with a very basic example, I'm unable to find any symbols in a compiled plugin. My setup looks like this:
/Users/blah/test-workspace/
src/
main/
main.go
plug/
plug.go
plug.go looks like this:
package main
type B struct {}
func main() {}
From the /Users/blah/test-workspace/ directory, I build this using:
GOPATH="/Users/blah/test-workspace" go build -buildmode plugin plug
This produces p.so inside the root of the GOPATH. Next I try to load this plugin via main/main.go:
package main
import (
"fmt"
"plugin"
"os"
)
func main() {
plugin, err := plugin.Open("plug.so")
if err != nil {
fmt.Printf("Error: %+v\n", err)
os.Exit(1)
}
fmt.Printf("%+v\n", plugin)
}
The output of this code is:
&{pluginpath:plug err: loaded:0xc420088060 syms:map[]}
As you can, the symbol map is empty. What am I doing wrong?
From the plugin docs
A symbol is any exported variable or function
You need to add an exported variable or function in order for your plugin to work.
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