Build constraints exclude all Go protobuffer generated files - go

I have updated go from v1.14 to 1.16 and now my generated go proto rise build errors.
My project structure:
project_folder
|-- proto_folder
|--data.proto
|--data.pb.go
|--client
|--client.go
|--main.go
When I try to import proto_folder in client/client.go:
import(
pb "project_folder/proto_folder"
"net/http"
"net/url"
)
func SendData(data *DataContainer) {
requestAddr := "http://localhost:8080/data"
out, err := proto.Marshal(data)
req, err := http.NewRequest("POST", requestAddr, bytes.NewBuffer(out))
//exec request with http client
.....
}
I get the error from the IDE (Goland):
Build constraints exclude all Go files in '/usr/local/go/src/project_folder/proto_folder'
When I try to build with go build I get this error:
package project_folder
imports project_folder/client
imports project_folder/proto_folder: no Go files in /usr/local/go/src/project_folder/proto_folder

Related

Golang using aws lambda error: InvokeInput not declared by package lambda

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),
},
)

build command-line-arguments: cannot load go-sql-driver/mysql

I got an error
build command-line-arguments: cannot load go-sql-driver/mysql: malformed module path "go-sql-driver/mysql": missing dot in first path element
while application code is
package main
import "database/sql"
import _ "go-sql-driver/mysql"
func main() {
db, err := sql.Open("mysql", "root:xxx#(127.0.0.1:3306)/dbname?parseTime=true")
err := db.Ping()
}
I'v done with go get -u github.com/go-sql-driver/mysql, and go.mod was updated, it looks fine.
Can someone help to solve this?
Change the import to:
import _ "github.com/go-sql-driver/mysql"
Go imports must have the full path to the imported module.

Why does proto marshal then unmarshal fail when it is run outside of project containing vendor directory?

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

No symbols in Go plugin

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.

My main.go file cannot see other files

I need some help understanding what is wrong with my file layout in a simple web application.
$GOPATH/src/example.com/myweb
I then have 2 files:
$GOPATH/src/example.com/myweb/main.go
$GOPATH/src/example.com/myweb/api.go
Both files have:
package main
The api.go file looks like:
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
type API struct {
URI string
Token string
Secret string
client *http.Client
}
...
My main.go file looks like:
package main
import (
"github.com/gorilla/mux"
"html/template"
"net/http"
)
var (
templates = template.Must(template.ParseFiles("views/home.html", "views/history.html", "views/incident.html"))
api = API{
URI: "http://localhost:3000",
Token: "abc",
Secret: "123",
}
)
func renderTemplate(w http.ResponseWriter, tmpl string, hp *HomePage) {
..
..
}
func WelcomeHandler(w http.ResponseWriter, r *http.Request) {
..
..
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", WelcomeHandler)
r.PathPrefix("/assets/").Handler(
http.StripPrefix("/assets/", http.FileServer(http.Dir("assets/"))))
http.ListenAndServe(":9000", r)
}
In the code I excluded, I basically use structs that are defined in my api.go file, and I get this error when doing:
go run main.go
# command-line-arguments
./main.go:16: undefined: API
./main.go:23: undefined: User
What exactly am I doing wrong here?
I tried changing the package name in api.go to myweb but that didn't help.
Am I suppose to use the package name myweb? Is just 1 file suppose to have main?
You're compiling only the main.go file. You should use:
go run main.go api.go
Or:
go run *.go
If you're writing a complex application, you might add everything to packages in subdirectories and have a single main.go file. For instance, etcd has an etcdmain subdirectory/package along with other subdirectories/packages. Something like:
/alarm
/auth
/cmd
/etcdmain
...
And the main.go file is simply:
package main
import "github.com/coreos/etcd/etcdmain"
func main() {
etcdmain.Main()
}
You are using golang workspace project, which is good for the structure for your application and it also standardize.
When we use the golang workspace, you can not run single go file. You need to call go build / go install.
Install
go install example.com/myweb
The command above will compile your main package on example.com/myweb. And the myweb executable binary will be placed on the GOPATH/bin. And you can run it manually.
Build
go build example.com/myweb
The command is similar to go install but the binary executable file will be placed on the current directory when you call the command, instead of on GOPATH/bin (unless your current directory is GOPATH/bin).
For more information please check this link.

Resources