Unable to use the protobuf package - go

It appears that I cannot import this package: github.com/golang/protobuf/proto
When I try to build or use go get I get:cannot load github.com/golang/protobuf/proto: module github.com/golang/protobuf#latest (v1.3.2) found, but does not contain package github.com/golang/protobuf/proto
It is a popular package, I am surprised it does not seem to be working.
https://godoc.org/github.com/golang/protobuf/proto#Marshal
Has anybody encountered this?
Update:
I am simply trying to import this:
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/golang/protobuf/proto"
)
GoLang does not resolve proto in the above path...
I try to install like this:
$ go get github.com/golang/protobuf/proto
go: finding github.com/golang/protobuf/proto latest
go get github.com/golang/protobuf/proto: module github.com/golang/protobuf#upgrade (v1.3.2) found, but does not contain package github.com/golang/protobuf/proto
Update2, not sure how the file helps but here it is:
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/golang/protobuf/proto"
"go_poc/plugins/com_styx_proto"
"io/ioutil"
"net/http"
"time"
)
func init() {
fmt.Println("styxBotDetect plugin is loaded!")
}
func (r registrable) RegisterHandlers(f func(
name string,
handler func(
context.Context,
map[string]interface{},
http.Handler) (http.Handler, error),
)) {
f(pluginName, r.registerHandlers)
}
func (r registrable) registerHandlers(ctx context.Context, extra map[string]interface{}, handler http.Handler) (http.Handler, error) {
// skipping some lines here
styxRqBytes, err := proto.Marshal(styxRq)
if err != nil {
http.Error(w, err.Error(), http.StatusNotAcceptable)
return
}
// more code

It turns out there was something wrong with the module cache, that's why the go tool was not able to fetch / update dependencies.
In such cases, clearing the module cache (might) help:
go clean -modcache

On the Terminal window, please run the following commands,
go clean -modcache
go get -u github.com/golang/protobuf/proto
Then run the following commands to get the packages downloaded and update in the .mod file
go mod init Version1
go mod tidy

Related

How to import go file from internal folder?

I have a go project whose structure is:
- internal
- client.go
main.go
go.mod
go.mod:
module github.com/zhaoyi0113/eml-transaction
go 1.17
require github.com/go-resty/resty/v2 v2.7.0
require golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect
client.go:
func SendTransaction() {
}
main.go
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
import "github.com/zhaoyi0113/eml-transaction/internal"
func main() {
SendTransaction()
}
The problem is that SendTransaction is invisible in main.go. When I run go build, I got below error:
./main.go:11:8: imported and not used: "github.com/zhaoyi0113/eml-transaction/internal"
./main.go:36:2: undefined: SendTransaction
./main.go:36:18: undefined: TransactionRequest
I don't understand why the import is not used. What is the right way to import it?
Just figured out how it works. I have to use internal as the prefix to call this method.
internal.SendTransaction(internal.TransactionRequest{})
``

Go run/build cannot find source files

I am trying to run a simple hello world style program that imports a print function from a separate custom package but Go is unable to find it despite teh correct $GOPATH etc being set.
What is missing that will make teh file be picked up?
etherk1ll#ubuntu:~/Development/GoWorkSpace/src/sonarparser$ echo $GOPATH
/home/etherk1ll/Development/GoWorkSpace/
etherk1ll#ubuntu:~/Development/GoWorkSpace/src/sonarparser$ pwd
/home/etherk1ll/Development/GoWorkSpace/src/sonarparser
etherk1ll#ubuntu:~/Development/GoWorkSpace/src/sonarparser$ ls
jsonparser.go main.go
etherk1ll#ubuntu:~/Development/GoWorkSpace/src/sonarparser$ go run main.go
main.go:5:2: cannot find package "sonarparser/jsonparser" in any of:
/usr/local/go/src/sonarparser/jsonparser (from $GOROOT)
/home/etherk1ll/Development/GoWorkSpace/src/sonarparser/jsonparser (from $GOPATH)
main.go
package main
import (
"fmt"
"jsonparser"
)
func main() {
fmt.Println("Hello world 1")
fmt.Println(jsonparser.HelloTwo)
}
jsonparser.go
package jsonparser
import "fmt"
func HelloTwo() {
fmt.Println("Hello world 2")
}
Because jsonparser.go and main.go are in the same package, Go requires those files to have the same package name. And because you defined the main function for the execution, the package must be "main".
Step 1: So you should rename jsonparser.go's package to main.
// jsonparser.go
package main
import "fmt"
func HelloTwo() {
fmt.Println("Hello world 2")
}
Step 2: You need to update main.go file to correct the import path:
// main.go
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello world 1")
HelloTwo()
}
Step 3: Then you run the following command (you must include all necessary files in the command)
go run main.go jsonparser.go

I can't import "cloud.google.com/go/datastore"

I can't understand why this :/
I tried go get -u ** with every url that I found.
Thanks
Golang:
$ go version
go version go1.13.3 windows/amd64
Source test:
package main
import (
"fmt"
"cloud.google.com/go/datastore"
)
var client *datastore.Client
func main() {
fmt.Println("Work")
}
Error:
$ go run main.go
# google.golang.org/grpc/internal/transport
..\..\..\..\google.golang.org\grpc\internal\transport\http_util.go:270:23: cannot use hf (type "vendor/golang.org/x/net/http2/hpack".HeaderField) as type
"golang.org/x/net/http2/hpack".HeaderField in argument to d.processHeaderField
..\..\..\..\google.golang.org\grpc\internal\transport\http_util.go:675:23: cannot use "golang.org/x/net/http2/hpack".NewDecoder(http2InitHeaderTableSize,
nil) (type *"golang.org/x/net/http2/hpack".Decoder) as type *"vendor/golang.org/x/net/http2/hpack".Decoder in assignment
Go requires you make use of any package that you import. In this case you are importing "cloud.google.com/go/datastore" but not doing anything with it. The global variable that you declared is also not being used. Since it seems you are just trying to test, so I would recommend you do something with it (atleast print it). Like-
package main
import (
"fmt"
"cloud.google.com/go/datastore"
)
var client *datastore.Client
func main() {
fmt.Println(client)
}

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.

Golang Package Import

I am attempting to get the following code to compile:
package main
import (
"fmt"
"code.google.com/p/go.text/unicode/norm"
)
func main() {
fmt.Println(norm.IsNormalString("ŋ̊"))
}
I have installed the unicode/norm package. I compile with the command:
go build -o ipa ipa.go
Unfortunately, I get the following error:
# command-line-arguments
./ipa.go:9: undefined: norm.IsNormalString
make: *** [ipa] Error 2
It seems that the package is being imported correctly, but I cannot access any of its members. I have tried changing the method from being called to another from norm, but I still get the error. This leads me to believe that I'm fundamentally misunderstanding something about go's package system.
func (Form) IsNormalString
func (f Form) IsNormalString(s string) bool
IsNormalString returns true if s == f(s).
IsNormalString is not a function, it's a method on type Form. For example,
package main
import (
"code.google.com/p/go.text/unicode/norm"
"fmt"
)
func main() {
fmt.Println(norm.NFC.IsNormalString("ŋ̊"))
}
Output:
true

Resources