Unable to install gRPC for go in mac os - go

While running:
brew install
I am receiving:
brew command not found
and while running:
go get -u github.com/golang/protobuf/protoc-gen-go
I am receiving:
go: found github.com/golang/protobuf/protoc-gen-go in github.com/golang/protobuf v1.4.2
go: google.golang.org/protobuf upgrade => v1.25.0
go: updating go.sum: open /usr/local/go/src/go.sum: permission denied
ksetti-mac-mini-4#KSETTis-Mini src % go get -u github.com/golang/protobuf/protoc-gen-go
go: found github.com/golang/protobuf/protoc-gen-go in github.com/golang/protobuf v1.4.2
go: google.golang.org/protobuf upgrade => v1.25.0
go: updating go.sum: open /usr/local/go/src/go.sum: permission denied
What am I doing wrong?

Is your GOPATH set to something like '~/go'? It's trying to write to a soure directory that you don't seem to have access to.
This is from my .bashrc
# GoLang Configurations
export GOPATH=$HOME/Golang
export PATH=$PATH:$GOPATH/bin

Related

Go package import cant be found

I'm new to Go and trying to import the jwx package found here. Using the following sample code. On Windows, I execute the following command in the root of my source.
go get github.com/lestrrat-go/jwx
go get github.com/lestrrat-go/jwx/jwk
The command "get go" passes:
but when I go into the import section of the go file I get the following error:
Any ideas?
Have a look at a fresh full working session below, hope it can help you find what it's wrong with your procedure.
Make sure we are using a recent Go version
$ go version
go version go1.18.1 linux/amd64
Make sure that GOPATH is unset
$ echo $GOPATH
$
Create and initialize a new module
$ mkdir /tmp/example
$ cd /tmp/example
$ go mod init example.com/example
go: creating new go.mod: module example.com/example
$
Write the test program
$ cat > main.go
package main
import (
"fmt"
"github.com/lestrrat-go/jwx/v2/jwk"
)
func main() {
fmt.Println(jwk.Cache{})
}
$
Download required modules
$ go mod tidy
go: downloading github.com/lestrrat-go/jwx/v2 v2.0.0
go: downloading github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1
go: downloading github.com/lestrrat-go/blackmagic v1.0.1
go: downloading github.com/lestrrat-go/httprc v1.0.1
go: downloading github.com/lestrrat-go/iter v1.0.2
go: downloading github.com/lestrrat-go/option v1.0.0
go: downloading github.com/goccy/go-json v0.9.7
go: downloading golang.org/x/crypto v0.0.0-20220214200702-86341886e292
go: downloading github.com/lestrrat-go/httpcc v1.0.1
go: downloading github.com/stretchr/testify v1.7.1
go: downloading gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
go: downloading github.com/davecgh/go-spew v1.1.0
go: downloading github.com/pmezard/go-difflib v1.0.0
$
Run the test program
$ go run example.com/example
{<nil>}
$

How do I run golint in docker build

I have the following in my docker-build file:
# Runs lint
RUN go get -u golang.org/x/lint/golint
RUN golint ./...
Doing this seems to not work, this is the output I get:
Step 34/38 : RUN go get -u golang.org/x/lint/golint
[01:41:31][Step 2/10] ---> Running in 23acc6d25de3
[01:41:33][Step 2/10] go: finding golang.org/x/lint latest
[01:41:33][Step 2/10] go: downloading golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
[01:41:33][Step 2/10] go: extracting golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
[01:41:33][Step 2/10] go: downloading golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3
[01:41:33][Step 2/10] go: extracting golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3
[01:41:33][Step 2/10] go: downloading golang.org/x/tools v0.0.0-20190624222133-a101b041ded4
[01:41:34][Step 2/10] go: extracting golang.org/x/tools v0.0.0-20190624222133-a101b041ded4
[01:41:34][Step 2/10] go: finding golang.org/x/tools v0.1.3
[01:41:35][Step 2/10] go: downloading golang.org/x/tools v0.1.3
[01:41:35][Step 2/10] go: extracting golang.org/x/tools v0.1.3
[01:41:45][Step 2/10] Removing intermediate container 23acc6d25de3
[01:41:45][Step 2/10] ---> 8bbf3cabfd41
[01:41:45][Step 2/10] Step 35/38 : RUN golint ./...
[01:41:45][Step 2/10] ---> Running in e3fe5ec9e62e
[01:41:45][Step 2/10] /bin/sh: golint: command not found
Thanks Jens! The answer was to either add $GOPATH/bin to path or to just use $GOPATH/bin (which is what I did). In particular the solution was:
# Run lint
RUN go get -u golang.org/x/lint/golint
RUN ${GOPATH}/bin/golint -set_exit_status ./...

How to solve vs code - gopls command is not available

The "gopls" command is not available. Run "go get -v golang.org/x/tools/gopls" to install.
I'm running on go 1.14. Not sure what will happen, if I follow the instruction.
Installing 1 tool at the configured GOBIN: /usr/local/go/bin in module mode.
gopls
Installing golang.org/x/tools/gopls FAILED
{
"killed": false,
"code": 1,
"signal": null,
"cmd": "/usr/local/go/bin/go get -v golang.org/x/tools/gopls",
"stdout": "",
"stderr": "go: downloading golang.org/x/tools/gopls v0.6.9\ngo: golang.org/x/tools/gopls upgrade => v0.6.9\ngo: downloading golang.org/x/tools v0.1.1-0.20210319172145-bda8f5cee399
...
1 tools failed to install.
gopls: failed to install gopls(golang.org/x/tools/gopls): Error: Command failed: /usr/local/go/bin/go get -v golang.org/x/tools/gopls
go: downloading golang.org/x/tools/gopls v0.6.9
go: golang.org/x/tools/gopls upgrade => v0.6.9
go: downloading golang.org/x/tools v0.1.1-0.20210319172145-bda8f5cee399
go: downloading golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c
go: downloading honnef.co/go/tools v0.1.1
go: downloading golang.org/x/mod v0.4.1
golang.org/x/mod/semver
...
go get golang.org/x/tools/gopls: copying /var/folders/gq/bwl3jmx562x5twchgxvb6mlh0000gn/T/go-build703164122/b001/exe/a.out: open /usr/local/go/bin/gopls: permission denied
no output
I had the same problem and solved it with:
go install -v golang.org/x/tools/gopls#latest
if you get the error:
x\tools#v0.1.13-0.20220811140653-b901dff69f70\internal\lsp\source\hover.go:23:2: module golang.org/x/text#latest found (v0.3.7), but does not contain package golang.org/x/text/unicode/runenames
try using:
go clean -modcache
go install -v golang.org/x/tools/gopls#latest
The extension depends on other tools to provide necessary features. gopls is one of the core tools necessary to provide language features like code completion, navigation, search, etc for go language. See the list of tools the extension depends on: https://github.com/golang/vscode-go/blob/master/docs/tools.md
"Install" will install the 'gopls'.
"Install All" will install all the missing tools.
If you are using go1.14, either click the button (or run "Go: Install/Update Tools" command from the command palette) or, install tools in module mode with the following command (not just go get because with go1.14, the go command will try to install the tool in GOPATH mode which isn't what you want.)
cd /tmp/
GO111MODULE=on go get golang.org/x/tools/gopls
Here I solved using the command brew install gopls

Error when trying to deploy Google Cloud function with Vendor directory

I tried with go113 and go111 but none of them works:
Directory structure looks like:
cool_project
- func.go
- func_test.go
utility
- helper.go
fancy
- fancy.go
go.mod # module cool_project
go.sum
.gcloudignore
vendor
...
When I try deploying wiht gcloud functions deploy ... it fails with: cannot find package cool_project/utility or cool_project/fancy.
I had to try with Vendor ebcause go.mod keeps failing with INVALID_ARGUMENT so after Googling around someone pointed this article
Note:
If I deploy with go111 and just the go.mod file (no vendor directory) it fails with cryptic error:
Deploying function (may take a while - up to 2 minutes)...failed.
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: cfb2
go: finding google.golang.org/grpc v1.27.0
go: finding golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5
go: finding github.com/golang/protobuf v1.3.3
go: finding golang.org/x/lint v0.0.0-20200130185559-910be7a94367
go: finding golang.org/x/tools v0.0.0-20191113191852-77e3bb0ad9e7
go: finding golang.org/x/time v0.0.0-20191024005414-555d28b269f0
go: finding google.golang.org/genproto v0.0.0-20200204135345-fa8e72b47b90
go: finding cloud.google.com/go/bigquery v1.4.0
go: finding golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
go: finding cloud.google.com/go v0.52.0
go: finding golang.org/x/tools v0.0.0-20200204074204-1cc6d1ef6c74
go: finding golang.org/x/mod v0.2.0
go: finding golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7
go: finding github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473
go: finding golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898
go: finding golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6
go: finding golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee
go: finding golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
go: finding github.com/google/go-cmp v0.4.0
go: finding golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8
go: finding golang.org/x/net v0.0.0-20190724013045-ca1201d0de80
go: finding google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1
go: finding golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e
go: finding golang.org/x/tools v0.0.0-20191216173652-a0e659d51361
go: finding golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e
go: finding golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
go: finding golang.org/x/exp v0.0.0-20191129062945-2f5052295587
go: finding golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f
go: finding golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f
go: finding golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
go: finding golang.org/x/sys v0.0.0-20200122134326-e047566fdf82
go: finding github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e
go: finding golang.org/x/tools v0.0.0-20200122220014-bf1340f18c4a
go: finding golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a
go: finding google.golang.org/genproto v0.0.0-20200122232147-0452cf42e150
go: finding golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa
go: finding google.golang.org/genproto v0.0.0-20200115191322-ca5a22157cba
go: finding golang.org/x/sys v0.0.0-20200113162924-86b910548bc1
go: finding golang.org/x/tools v0.0.0-20200117161641-43d50277825c
go: finding github.com/google/pprof v0.0.0-20191218002539-d4f498aebedc
go: finding golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e
go: finding github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
go: finding github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1
go: finding github.com/chzyer/logex v1.1.10
go: finding github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6
go: downloading github.com/pkg/errors v0.9.1
go: downloading google.golang.org/api v0.17.0
go: downloading cloud.google.com/go v0.52.0
go: downloading github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575
go: downloading gopkg.in/yaml.v2 v2.2.8
go: downloading cloud.google.com/go/storage v1.5.0
go: downloading cloud.google.com/go/pubsub v1.2.0
go: downloading google.golang.org/genproto v0.0.0-20200204135345-fa8e72b47b90
go: downloading google.golang.org/grpc v1.27.0
go: downloading github.com/golang/protobuf v1.3.3
go: downloading golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
go: downloading go.opencensus.io v0.22.3
go: downloading golang.org/x/net v0.0.0-20200202094626-16171245cfb2
go: downloading github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e
go: downloading golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5
go: downloading github.com/googleapis/gax-go/v2 v2.0.5
go: downloading golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
go: downloading github.com/google/go-cmp v0.4.0
# cool_project
files/foo.go:86:69: undefined: "github.com/pkg/errors".Unwrap
files/bar.go:90:70: undefined: "github.com/pkg/errors".Unwrap
And with go113 and go.mod file it is giving not at all helpful error:
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: Build error details not available
Logs shows:
INVALID_ARGUMENT
Ran deploy cmd:
gcloud functions deploy cool_proj113 --trigger-topic test_cool_proj --runtime go113 --entry-point HelloPubSub --source=$PWD/cool_project --verbosity="debug"
DEBUG: (gcloud.functions.deploy) OperationError: code=4, message=Build failed: srv/gopath/src/p/vendor/gcpfun/func.go:21:2: cannot find package "cool_project/fancy" in any of:
/tmp/staging/srv/gopath/src/p/vendor/vendor/cool_project/fancy (vendor tree)
/tmp/staging/srv/gopath/src/p/vendor/cool_project/fancy
/usr/local/go/src/cool_project/fancy (from $GOROOT)
/tmp/staging/srv/gopath/src/cool_project/fancy (from $GOPATH)
srv/gopath/src/p/vendor/gcpfun/func.go:20:2: cannot find package "cool_project/utility" in any of:
/tmp/staging/srv/gopath/src/p/vendor/vendor/cool_project/utility (vendor tree)
/tmp/staging/srv/gopath/src/p/vendor/cool_project/utility
/usr/local/go/src/cool_project/utility (from $GOROOT)
/tmp/staging/srv/gopath/src/cool_project/utility (from $GOPATH)
Traceback (most recent call last):
File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/calliope/cli.py", line 981, in Execute
resources = calliope_command.Run(cli=self, args=args)
File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/calliope/backend.py", line 807, in Run
resources = command_instance.Run(args)
File "/usr/lib/google-cloud-sdk/lib/surface/functions/deploy.py", line 309, in Run
return _Run(args, track=self.ReleaseTrack())
File "/usr/lib/google-cloud-sdk/lib/surface/functions/deploy.py", line 265, in _Run
op, do_every_poll=TryToSetInvokerPermission)
File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/api_lib/functions/util.py", line 306, in CatchHTTPErrorRaiseHTTPExceptionFn
return func(*args, **kwargs)
File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/api_lib/functions/util.py", line 353, in WaitForFunctionUpdateOperation
do_every_poll=do_every_poll)
File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/api_lib/functions/operations.py", line 137, in Wait
_WaitForOperation(client, request, notice, do_every_poll)
File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/api_lib/functions/operations.py", line 111, in _WaitForOperation
sleep_ms=SLEEP_MS)
File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/core/util/retry.py", line 219, in RetryOnResult
result = func(*args, **kwargs)
File "/usr/lib/google-cloud-sdk/lib/googlecloudsdk/api_lib/functions/operations.py", line 70, in _GetOperationStatus
raise exceptions.FunctionsError(OperationErrorToString(op.error))
Sigh! FWIW I did test go unit test and things work fine with go113.
I have found 2 (very hacky) ways to fix this issue. My particular case was that I have a private repository that GCP can't access. They say I need to create a vendor directory at the bottom of this page (today):
https://cloud.google.com/functions/docs/writing/specifying-dependencies-go
So I did but then I got the error you're reporting above.
Bad Solution 1:
Put everything in the main directory and rename every package to the entrypoint function's package name.
Hacky Solution 2:
This is literally following the first line of the error:
DEBUG: (gcloud.functions.deploy) OperationError: code=4, message=Build failed: srv/gopath/src/p/vendor/gcpfun/func.go:21:2: cannot find package "cool_project/fancy" in any of:
/tmp/staging/srv/gopath/src/p/vendor/vendor/cool_project/fancy (vendor tree)
In this case do a:
go mod vendor
Then in the vendor directory create a folder with the name of the go mod package name, in your case that would be 'cool_project'. Then copy and paste the subdir folders into that directory (e.g.: vendor/cool_project/fancy/*.go) and then deploy it again.
Which directory are you in when you try to deploy? Are you inside cool_project? You need to make sure you're inside the directory that contains you go.mod file.

How can I use Go modules to pull a dependency by commit hash or branch?

I'm trying to upgrade my project to use Go modules for dependency management. I recognize this a new/experimental feature. I'm getting an error: cannot find module providing package.
My go.mod currently reads:
require (
github.com/bugsnag/bugsnag-go v1.5.1
github.com/bwmarrin/discordgo v0.19.0
github.com/jonas747/dshardmanager v0.0.0-20180911185241-9e4282faed43
)
My Dockerfile:
FROM golang:1.12-alpine
RUN mkdir /app
WORKDIR /app
ADD src/ /app
ENV CGO_ENABLED=0
# Building requires git because we're pulling a dependency by commit hash
RUN apk add --no-cache --update git \
&& go build ./... \
&& apk del git
CMD ["/app/main"]
When building, it fails with:
cannot load github.com/bugsnag/panicwrap: cannot find module providing package github.com/bugsnag/panicwrap
When I look at https://github.com/bugsnag/bugsnag-go I don't see a go.mod file. Is this error indicating that this dependency doesn't support modules?
You're doing something wrong, because it works. It does not need a go.mod in order to work with go.mod-based applications:
nrxr at lise in ~/code/src/github.com/nrxr/stack
$ go mod init
go: creating new go.mod: module github.com/nrxr/stack
nrxr at lise in ~/code/src/github.com/nrxr/stack
$ go get github.com/bugsnag/bugsnag-go
go: finding github.com/bugsnag/bugsnag-go v1.5.1
go: downloading github.com/bugsnag/bugsnag-go v1.5.1
go: extracting github.com/bugsnag/bugsnag-go v1.5.1
go: finding github.com/bugsnag/panicwrap v1.2.0
go: downloading github.com/bugsnag/panicwrap v1.2.0
go: finding github.com/gofrs/uuid v3.2.0+incompatible
go: downloading github.com/gofrs/uuid v3.2.0+incompatible
go: extracting github.com/bugsnag/panicwrap v1.2.0
go: extracting github.com/gofrs/uuid v3.2.0+incompatible
go: finding github.com/kardianos/osext latest
go: downloading github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
go: extracting github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
nrxr at lise in ~/code/src/github.com/nrxr/stack
$ cat go.mod
module github.com/nrxr/stack
go 1.12
require (
github.com/bugsnag/bugsnag-go v1.5.1 // indirect
github.com/bugsnag/panicwrap v1.2.0 // indirect
github.com/gofrs/uuid v3.2.0+incompatible // indirect
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
)
Maybe try again? Try with what I just used here.

Resources