I use command go get github.com/gorilla/mux. I made http server using Golang, and I run this program :
package main
import (
"fmt"
"html"
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", Index)
log.Fatal(http.ListenAndServe(":8080", router))
}
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
But I conflict this error :
/usr/local/go/bin/go build -i [/Users/imac/go/src]
http.go:9:5: cannot find package "github.com/gorilla/mux" in any of:
/usr/local/go/src/github.com/gorilla/mux (from $GOROOT)
($GOPATH not set)
Error: process exited with code 1.
My Go environment is here :
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/imac/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/v9/fkc_t97s5v1g9sr938zzvxvh0000gn/T/go-build096571864=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
I fight with this error for a week, But I can't find out solution. Please help me.
Could you try this steps to debug it:
ls -l /usr/local/go/src/github.com | grep gorilla
cd $GOPATH
go list ... | grep gorilla
if you din't see gorilla in the above two command, then you need to install it:
go get -v -u github.com/gorilla/mux
Please run this: export PATH=$PATH:$GOPATH/bin
How about running go run main.go ? is that working, if yes you should be able to do go build from your project path.
I wish this helpful.
You may off the 'mod'.
$ export GO111MODULE=off
This works for me...
fire command
> go mod init <your-directoryNane-where-main.go-exits>
fire command > go get github.com/gorilla/mux
after firing this both commands you will be able see 2 files :
go.mod
go.sum
finally, Close VS code and open again, the error will be resolved
Just remove quotes like this:
go get github.com/gorilla/mux
If you are using VS Code as your IDE and facing this problem:
VS Code uses $HOME/go as your default GOPATH - if you export another GOPATH you running into this trouble.
How to solve:
Use the VS Code internal terminal and navigate to your project folder: cd prjectFolder. Type go env and check if the GOPATH entry is the same as you get when you use cmd+t and then >Go: Current GOPATH
If it doesn't fit, add in your user settings:
"go.gopath": "/some/path"
where /some/path is the same path you export in you shell, zsh and so on.
Hope this helps.
I try to remove github.com/gorilla and github.com/peterbourgon directory, then, retry: make, it works.
<pre>
fail log:
mac#user:~/TempPlace/temp/ngrok% make
go fmt ngrok/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
-debug=true \
-o=src/ngrok/client/assets/assets_debug.go \
assets/client/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
-debug=true \
-o=src/ngrok/server/assets/assets_debug.go \
assets/server/...
go get -tags 'debug' -d -v ngrok/...
src/ngrok/server/config.go:16:2: no Go files in /Users/apple/TempPlace/temp/ngrok/src/github.com/gorilla/mux
src/ngrok/server/config.go:17:2: no Go files in /Users/apple/TempPlace/temp/ngrok/src/github.com/peterbourgon/diskv
make: *** [deps] Error 1
success log:
mac#user:~/TempPlace/temp/ngrok% make
go fmt ngrok/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
-debug=true \
-o=src/ngrok/client/assets/assets_debug.go \
assets/client/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
-debug=true \
-o=src/ngrok/server/assets/assets_debug.go \
assets/server/...
go get -tags 'debug' -d -v ngrok/...
github.com/gorilla/websocket (download)
github.com/gorilla/mux (download)
src/ngrok/server/config.go:17:2: no Go files in /Users/apple/TempPlace/temp/ngrok/src/github.com/peterbourgon/diskv
make: *** [deps] Error 1
mac#user:~/TempPlace/temp/ngrok% make
go fmt ngrok/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
-debug=true \
-o=src/ngrok/client/assets/assets_debug.go \
assets/client/...
go get github.com/jteeuwen/go-bindata
GOOS="" GOARCH="" go install github.com/jteeuwen/go-bindata/go-bindata
bin/go-bindata -nomemcopy -pkg=assets -tags=debug \
-debug=true \
-o=src/ngrok/server/assets/assets_debug.go \
assets/server/...
go get -tags 'debug' -d -v ngrok/...
github.com/peterbourgon/diskv (download)
github.com/google/btree (download)
go install -tags 'debug' ngrok/main/ngrok
go install -ldflags "-s" -tags 'debug' ngrok/main/ngrokd
</pre>
Try go build /Users/imac/go/src/project
because I see you try to use go build under /Users/imac/go/src
Maybe this can help other users running in Windows.
In my case I had to create two symlinks:
1. run cmd as administrator
2. cd %gopath%
3. mklink /D src pkg\mod
this creates a symlink between src and pkg\mod
4. cd src\github.com\gorilla
Here you'll notice that the mux package might be listed as mux#v1.8.0
5. mklink /D mux mux#v1.8.0
With this, go will be able to find github.com/gorilla/mux
under %gopath%\src\github.com\gorilla\mux
Finally, you have to set GO111Module to off
set GO111Module=off
and now you can build your app:
go build app.go
Related
I'm building a go 1.20 program that use podman bindings (v4), one of its dependencies require CGO.
Dynamically build & LD
Dynamically building the project works on alpine linux (golang:1.20-alpine), ld returns the following:
/lib/ld-musl-x86_64.so.1 (0x7f741c064000)
libgpgme.so.11 => /usr/lib/libgpgme.so.11 (0x7f741c01f000)
libdevmapper.so.1.02 => /lib/libdevmapper.so.1.02 (0x7f741bfd4000)
libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7f741c064000)
libassuan.so.0 => /usr/lib/libassuan.so.0 (0x7f741bfc1000)
libgpg-error.so.0 => /usr/lib/libgpg-error.so.0 (0x7f741bfa0000)
The command I used is CGO_ENABLED=1 go build -o myprog ./cmd/cli/main.go
Trying statically build
But when I try to statically build (for use in a scratch container) with go build -ldflags '-linkmode external -w -extldflags "-static"' -o myprog ./cmd/cli/main.go I have this error:
/app # go build -ldflags '-linkmode external -w -extldflags "-static" ' -o myprog ./cmd/cli/main.go
# command-line-arguments
/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/lib/gcc/x86_64-alpine-linux-musl/12.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -ldevmapper: No such file or directory
collect2: error: ld returned 1 exit status
Dependencies installation
But It's installed along with other dependencies previously in the container:
RUN apk add --no-cache make gcc musl-dev lvm2-dev gpgme-dev btrfs-progs-dev
What I've tried
I tried building from the ubuntu-based tag for go (golang:1.20), but same it can't find devmapper.
Explicitly setting to CGO_ENABLED to 0 or 1 don't change anything when trying to build statically.
I passed -v to go build to know how it's linking, here is the result:
/app # CC=gcc go build -ldflags '-linkmode external -w -extldflags "-static" -v' -o myprog ./cmd/cli/main.go
# command-line-arguments
HEADER = -H5 -T0x401000 -R0x1000
host link: "gcc" "-m64" "-o" "/tmp/go-build2898353667/b001/exe/a.out" "-static" "-Wl,--compress-debug-sections=zlib" "/tmp/go-link-3857943538/go.o" "/tmp/go-link-3857943538/000000.o" [...] "/tmp/go-link-3857943538/000053.o" "-O2" "-g" "-lresolv" "-O2" "-g" "-O2" "-g" "-lpthread" "-O2" "-g" "-O2" "-g" "-O2" "-g" "-ldl" "-O2" "-g" "-O2" "-g" "-lgpgme" "-O2" "-g" "-O2" "-g" "-L/lib" "-ldevmapper" "-O2" "-g" "-O2" "-g" "-O2" "-g" "-no-pie" "-static"
/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/lib/gcc/x86_64-alpine-linux-musl/12.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -ldevmapper: No such file or directory
collect2: error: ld returned 1 exit status
libdevmapper is in several location, including in /lib (as seen in the linker) at /lib/libdevmapper.so.1.02
Reproduce
main.go:
package main
import (
"context"
"fmt"
"os"
"github.com/containers/podman/v2/pkg/bindings"
"github.com/containers/podman/v2/pkg/bindings/images"
)
func main() {
// Get Podman socket location
sock_dir := os.Getenv("XDG_RUNTIME_DIR")
socket := "unix:" + sock_dir + "/podman/podman.sock"
// Connect to Podman socket
ctx, err := bindings.NewConnection(context.Background(), socket)
if err != nil {
panic(err)
}
// list images
imgs, err := images.List(ctx, nil, nil)
if err != nil {
panic(err)
}
for _, img := range imgs {
fmt.Printf("Names: %#v\n", img.Names)
}
}
Containerfile (or Dockerfile):
FROM --platform=linux/amd64 docker.io/library/golang:1.20-alpine AS builder
RUN apk add --no-cache make gcc musl-dev lvm2-dev gpgme-dev btrfs-progs-dev
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
# Dont work
RUN go build -ldflags '-linkmode external -w -extldflags "-static" ' -o myprog ./main.go
# this works, but dynamically linked
# RUN go build -o myprog ./main.go
Then run:
go init sof-75459376
go mod tidy
# fail
podman build -t sof/75459376:latest .
I have to compile a Go service for Ubuntu ARM-v7
When I compile it with
GOARCH=arm GOARM=7 go build -v -o release/edge_to_bc -ldflags '-s -w -extldflags "-static"' ./...
I get:
gitlab.com/company/edge_to_bc/vendor/github.com/hyperledger/fabric/bccsp/pkcs11
# gitlab.com/company/edge_to_bc/vendor/github.com/hyperledger/fabric/bccsp/pkcs11
vendor/github.com/hyperledger/fabric/bccsp/pkcs11/impl.go:82:12: undefined: pkcs11.Ctx
vendor/github.com/hyperledger/fabric/bccsp/pkcs11/impl.go:83:16: undefined: pkcs11.SessionHandle
gitlab.com/company/edge_to_bc/vendor/github.com/mattn/go-sqlite3
# gitlab.com/company/edge_to_bc/vendor/github.com/mattn/go-sqlite3
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:18:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:26:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:27:17: undefined: namedValue
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:29:13: undefined: namedValue
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:35:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:44:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:49:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:54:10: undefined: SQLiteStmt
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:63:10: undefined: SQLiteStmt
vendor/github.com/mattn/go-sqlite3/tracecallback_noimpl.go:8:10: undefined: SQLiteConn
vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go:29:13: too many errors
How should I fix it ?
Go-sqlite3 is cgo package.
If you want to build your app using go-sqlite3, you need gcc.
However, after you have built and installed go-sqlite3 with go install github.com/mattn/go-sqlite3 (which requires gcc), you can build your app without relying on gcc in future.
Important: because this is a CGO enabled package you are required to set the environment variable CGO_ENABLED=1 and have a gcc compile present within your path.
Source
Cross compiling from Ubuntu to ARM7:
sudo apt install \
libc6-armel-cross \
libc6-dev-armel-cross \
binutils-arm-linux-gnueabi \
libncurses5-dev \
gcc-arm-linux-gnueabihf
env CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ \
CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 \
go build -v
Using a docker image:
# Install docker
$ curl -fsSL https://get.docker.com -o get-docker.sh | sudo sh -
$ sudo usermod -aG docker your-user
# Go to your project folder
$ cd your-project-folder
# Compile
$ docker run --rm \
-v /tmp/.docker/go:/go \
-v /tmp/.docker/go-build:/root/.cache/go-build \
-v $PWD:$PWD \
-w $PWD \
filipeandre/go-compiler-to-arm7:1.12 \
go build -v
Dockerfile
I am unable to dockerize and use a utility written in c in go.
I have run this program locally without docker and it works
I tried using gccgo like so go build -compiler gccgo -gccgoflags -static-libgo but I get the same error
The preamble that calls the C functions looks like so:
/*
#cgo amd64 x86 LDFLAGS: -L. -lsomelib -lsomeotherlib
#include <stdio.h>
#include <stdlib.h>
#include "someheader.h"
*/
My docker file looks like so:
FROM golang:1.12 AS build
WORKDIR /go/src/app
COPY . .
ENV GOOS=linux
ENV GOARCH=amd64
ENV CGO_LDFLAGS_ALLOW='-linkmode external -extldflags -static-libgcc'
COPY packageFolder $GOPATH/src/packageFolder
COPY mainPackage $GOPATH/src/mainPackage
RUN cd packageFolder
RUN go get -d -v ./...
RUN CGO_ENABLED=1 go build --ldflags '-linkmode external -extldflags -static-libgcc' -o $GOPATH/pkg/linux_amd64/packageFolder.a -x
RUN cd ../packageFolder
RUN go get -d -v ./...
RUN CGO_ENABLED=1 go build --ldflags '-linkmode external -extldflags -static-libgcc' -o $GOPATH/pkg/linux_amd64/mainPackage.a -x
RUN cd ..
RUN go get -d -v ./...
RUN go build -a -x
FROM ourPackager:latest AS packager
WORKDIR /
COPY ./resources ./resources/
RUN appman-packager create-package "package.tar.gz" ./resources
FROM scratch AS runtime
COPY --from=build /go/src/app/app /
COPY --from=packager "/package.tar.gz" ./resources/
EXPOSE 8080/tcp
ENTRYPOINT ["/app"]
I keep running into standard_init_linux.go:207: exec user process caused "no such file or directory" when I do a docker run
What am I missing?
I was able to fix it with Mark's suggestion. Using a Golang image for runtime exposed the actual problem of the shared object file not being packaged. So I copy it to /usr/lib/x86_64-linux-gnu in my runtime. I ended up using ubuntu:18.04 instead of the Golang image at runtime
FROM golang:1.12 AS build
WORKDIR /go/src/app
COPY . .
ENV GOOS=linux
ENV GOARCH=amd64
ENV CGO_ENABLED=1
COPY acrcloud $GOPATH/src/packageFolder
COPY musicrec $GOPATH/src/mainPackage
RUN cd packageFolder
RUN go get -d -v ./...
RUN go build -o $GOPATH/pkg/linux_amd64/packageFolder -x
RUN cd ../mainPackage
RUN go get -d -v ./...
RUN go build -o $GOPATH/pkg/linux_amd64/mainPackage -x
RUN cd ..
RUN go get -d -v ./...
RUN go build -a -x
FROM ourPackager:latest AS packager
WORKDIR /
COPY ./resources ./resources/
RUN appman-packager create-package "package.tar.gz" ./resources
FROM ubuntu:18.04 AS runtime
COPY --from=build /go/src/app/app /
COPY --from=build /go/src/app/myExternalTool.so /usr/lib/x86_64-linux-gnu
COPY --from=packager "/package.tar.gz" ./resources/
EXPOSE 8080/tcp
ENTRYPOINT ["/app"]
There are two folds to this problem:
1) Go install not able to generate binary
go version go1.7.3 linux/amd64
go env:
GOARCH="amd64" GOBIN="" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/opt/gopath" GORACE="" GOROOT="/opt/go" GOTOOLDIR="/opt/go/pkg/tool/linux_amd64" CC="gcc" GOGCCFLAGS="-fPIC
-m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build831334660=/tmp/go-build -gno-record-gcc-switches" CXX="g++" CGO_ENABLED="1"
Source of the program: https://github.com/hyperledger/fabric
Working dir: /opt/gopath/src/github.com/hyperledger/fabric
Main program to build: /opt/gopath/src/github.com/hyperledger/fabric/peer/main.go
Build command:
CGO_ENABLED=0 GOBIN=/opt/gopath/bin go install -x -ldflags " -X github.com/hyperledger/fabric/common/metadata.Version=1.0.0-snapshot-d6fbfcf -X github.com/hyperledger/fabric/common/metadata.BaseVersion=0.3.0 -X github.com/hyperledger/fabric/common/metadata.BaseDockerLabel=org.hyperledger.fabric -X github.com/hyperledger/fabric/common/metadata.DockerNamespace=hyperledger -X github.com/hyperledger/fabric/common/metadata.BaseDockerNamespace=hyperledger -linkmode external -extldflags -static -lpthread" github.com/hyperledger/fabric/peer
I expect the binary built in /opt/gopath/bin but there was nothing there.
It shows on the console that series of .a files were built and moved to /opt/gopath/pkg/linux_amd64/
Eventually program stopped and no binary was built.
This is true regardless CGO_ENABLED was set to 0 or 1.
Somehow the program thoughts it was done, without linking the object files and building the binary? Any other debugging measures that I can use to look further into it?
2) The 'Go install' command was taken from Makefile
#$(DRUN) \
-v $(abspath build/docker/bin):/opt/gopath/bin \
-v $(abspath build/docker/$(TARGET)/pkg):/opt/gopath/pkg \
hyperledger/fabric-baseimage:$(BASE_DOCKER_TAG) \
go install -compiler gccgo -ldflags "$(DOCKER_GO_LDFLAGS)" $(pkgmap.$(#F))
where
DRUN = docker run -i --rm $(DOCKER_RUN_FLAGS) \
-v $(abspath .):/opt/gopath/src/$(PKGNAME) \
-w /opt/gopath/src/$(PKGNAME)
The 'Go install' command was run inside a docker container, which was started the same way the Makefile specified. The only difference was that I was doing it manually, in two shots, i.e. start docker container + go install. If I were to build it with Makefile in one shot, it would work just fine.
Is there any catch/caveat on using this 'docker run <image> go install <args>' style command?
I'm running golang in a docker container. And 'go tool' is unable to find 'vet'. Could you give me ideas on how to debug this?
I've used the Dockerfile for 1.5 as a template. https://github.com/docker-library/golang/blob/51d6eacd41fe80d41105142b9ad32f575082970f/1.5/Dockerfile
ENV GOLANG_VERSION 1.5.1
ENV GOLANG_DOWNLOAD_URL https://golang.org/dl/go$GOLANG_VERSION.linux- amd64.tar.gz
ENV GOLANG_DOWNLOAD_SHA1 46eecd290d8803887dec718c691cc243f2175fe0
RUN curl -fsSL "$GOLANG_DOWNLOAD_URL" -o golang.tar.gz \
&& echo "$GOLANG_DOWNLOAD_SHA1 golang.tar.gz" | sha1sum -c - \
&& tar -C /usr/local -xzf golang.tar.gz \
&& rm golang.tar.gz
ENV GOPATH /go
ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH
However, when I install govet with
go get golang.org/x/tools/cmd/vet
and try
bash-4.3# go tool vet
go tool: no such tool "vet"
I have the following go environment set up:
$PATH includes $GOPATH/bin /usr/lib/go/bin:/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
bash# go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT=""
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
bash# ls $GOPATH/bin
fgt go-junit-report godep golint mt-content-blogs vet
bash# ls $GOROOT/bin/
go gofmt
The crux of the issue is that go tools does not list vet, even after installing it with go get golang.org/x/tools/cmd/vet
bash# go tool
addr2line
api
asm
cgo
compile
dist
doc
fix
link
nm
objdump
pack
pprof
trace
yacc
Warning: starting Go 1.12 (February 2019, 3.5 years later), go tool vet won't be available at all. Only go vet.
See go 1.12 release notes:
The go vet command has been rewritten to serve as the base for a range of different source code analysis tools. See the golang.org/x/tools/go/analysis package for details.
A side-effect is that go tool vet is no longer supported.
External tools that use go tool vet must be changed to use go vet.
Using go vet instead of go tool vet should work with all supported versions of Go.
As part of this change, the experimental -shadow option is no longer available with go vet.
Checking for variable shadowing may now be done using:
go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
go vet -vettool=$(which shadow)
Figured out the issue. It appears that I had missed installing go tools on the base docker image that I was using.
RUN apk --update-cache --allow-untrusted \
--repository http://dl-3.alpinelinux.org/alpine/edge/community/ \
--arch=x86_64 add \
go=${GOLANG_VERSION}-r3 \
go-tools=${GOLANG_VERSION}-r3 \
git \
&& rm -rf /var/cache/apk/* \
&& mkdir -p /go/src /go/bin \
&& chmod -R 777 /go