here is a simple program:
package main
import (
"fmt"
"net"
)
func main() {
nameserver, _ := net.LookupNS("mylab.local")
for _, ns := range nameserver {
addrs, errIP := net.LookupIP(ns.Host)
if errIP == nil {
fmt.Printf("%s\n",addrs[0].String())
} else {
fmt.Printf("%s\n", errIP.Error())
}
}
}
Here is the ouput compiled with GOARCH=amd64:
$ GOOS=linux GOARCH=amd64 go build -o test test.go
$ ./test
?
The output compiled with GOARCH=386:
$ GOOS=linux GOARCH=386 go build -o test test.go
$ ./test
192.168.56.3
I'm working on a "Ubuntu 18.04.6 LTS".
Is there something obvious I missed ?
Thank you
Update 1: I updated the code in order to print the errIP
The output compiled GOARCH=amd64:
$ GOOS=linux GOARCH=amd64 go build -o test test.go
$ ./test
lookup dc01.mylab.local.: no such host
$ ping -c1 dc01.mylab.local
PING dc01.mylab.local (192.168.56.3) 56(84) bytes of data.
64 bytes from dc01.mylab.local (192.168.56.3): icmp_seq=1 ttl=128 time=0.561 ms
Update 2: #hobbs
Here is the output with the environment var:
$ export GODEBUG=netdns=1
$ GOOS=linux GOARCH=amd64 go build -o test test.go
$./test
go package net: dynamic selection of DNS resolver
&{dc01.mylab.local.}
lookup dc01.mylab.local.: no such host
Update 3: Ok solved, thank you hobbs. I found that with arch 386, the resolver used was go. So after that I had a look at the documentation of the net package. I compiled as follow:
$ export GODEBUG=netdns=go
$ GOOS=linux GOARCH=amd64 go build -o test test.go
$ ./test
192.168.56.3
Related
when use the gccgo build one single go file is ok, but whn I build a large multi custom pacage go mod project, the project have some sub package(such as app/ app/core/server etc) not build success.
how to fix this? anyhelp? I build with -x and see some debug info.
go build -x -gccgoflags -Wl,-R,/opt/gccgo/lib64/ -compiler gccgo -o bin/app main.go
the error is:
WORK=/tmp/go-build1609005358
mkdir -p $WORK/b001/
cd $WORK
/opt/gccgo/bin/gccgo -fgo-importcfg=/dev/null -c -x c - -o /dev/null || true
mkdir -p $WORK/b001/_importcfgroot_/github.com/gin-gonic
ln -s /home/liangqi1/.cache/go-build/8a/8ae3d6bb3097698b7be6547599d7a61c9c5c54cca0be55c007ca1c8386d1188c-d $WORK/b001/_importcfgroot_/github.com/gin-gonic/libgin.a
mkdir -p $WORK/b001/_importcfgroot_/github.com/logrusorgru
ln -s /home/liangqi1/.cache/go-build/6f/6fb602f3c990310188a3e921909d152810f07d50f1ab4c621ab71b87afd4942d-d $WORK/b001/_importcfgroot_/github.com/logrusorgru/libaurora.a
mkdir -p $WORK/b001/_importcfgroot_/go.uber.org
ln -s /home/liangqi1/.cache/go-build/c0/c0ac9f2f0ebb74e0997bfa1d72d21a79ce906ed736197f83402365203094c3a9-d $WORK/b001/_importcfgroot_/go.uber.org/libautomaxprocs.a
/opt/gccgo/bin/gccgo -ffile-prefix-map=a=b -c -x c - -o /dev/null || true
cd /home/liangqi1/gccgo_demo
/opt/gccgo/bin/gccgo -c -g -m64 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -fgo-relative-import-path=_/home/liangqi1/gccgo_demo -o $WORK/b001/_go_.o -I $WORK/b001/_importcfgroot_ -Wl,-R,/opt/gccgo/lib64/ ./main.go
# command-line-arguments
./main.go:7:23: 错误:import file ‘gccgo_demo/app’ not found
7 | "gccgo_demo/app"
| ^
./main.go:8:35: 错误:import file ‘gccgo_demo/app/core/server’ not found
8 | "gccgo_demo/app/core/server"
| ^
./main.go:24:19: 错误:reference to undefined name ‘app’
24 | if err := app.Init(prjHome); err != nil {
| ^
./main.go:29:12: 错误:reference to undefined name ‘app’
29 | if app.IsProd() {
| ^
./main.go:35:9: 错误:reference to undefined name ‘server’
35 | server.Run()
| ^
the gccgo just clone from gcc git:
gccgo (GCC) 12.0.1 20220217
go version is 1.16.4
my project struct like flow:
gccgo_demo
├── app
│ ├── app.go
│ ...
├── go.mod
├── go.sum
├── main.go
... other code
the go mod name is gccgo_demo so , the app/app.go has some import like gccgo_demo/app path.
I don't see your code but I guess the problem that you do not pass the include path.
The cgo tool will always invoke the C compiler with the source file's directory in the include path; i.e. -I${SRCDIR} is always implied. This means that if a header file foo/bar.h exists both in the source directory and also in the system include directory (or some other place specified by a -I flag), then "#include <foo/bar.h>" will always find the local version in preference to any other version.
So just do smth like
go build -x -gccgoflags -I/path/to/your/file -Wl,-R,/opt/gccgo/lib64/ -compiler gccgo -o bin/app main.go
More details https://pkg.go.dev/cmd/cgo
I am trying to build deterministic go executables. My build script is really simple:
#!/bin/bash
BASEPATH=$(git rev-parse --show-toplevel)
cd "$BASEPATH/lambda"
GOOS=linux GOARCH=amd64 GOFLAGS="-trimpath" GOPROXY=https://proxy.golang.org,direct go build
ls -la cdk-sops-secrets
touch -t 202002020000 cdk-sops-secrets
chmod 755 cdk-sops-secrets
shasum cdk-sops-secrets
ls -la cdk-sops-secrets
this runs fine, but produces different binarys/executables on my m1 mac and in the github actions run. They differ in size and shasum. Even if I use the same docker image (apart from that one is arm and the other is amd64).
Is it possible to build a shasum / byte-by-byte identical version of my code?
This script builds the same versions in CI and locally
#!/bin/bash
echo "GOROOT: $GOROOT / GOPATH: $GOPATH"
BASEPATH=$(git rev-parse --show-toplevel)
cd "$BASEPATH/lambda"
export GOOS=linux
export GOARCH=amd64
export GOPROXY=https://proxy.golang.org,direct
export CGO_ENABLED=0
go build -trimpath -buildvcs=false -ldflags="-s -w -buildid="
ls -la cdk-sops-secrets
shasum cdk-sops-secrets
touch -t 202002020000 cdk-sops-secrets
chmod 755 cdk-sops-secrets
ls -la cdk-sops-secrets
shasum cdk-sops-secrets
I'm encountering an error with a recently-compiled golang compiler, but only as an unprivileged user. I'm hoping someone could provide advice on further troubleshooting ideas.
As root:
$ cd $HOME && pwd
/root
$ mkdir -pv $HOME/go/src/hello
/bin/mkdir: created directory 'go'
/bin/mkdir: created directory 'go/src'
/bin/mkdir: created directory 'go/src/hello'
$ cd go/src/hello
$ cat > hello.go << EOF
> package main
>
> import "fmt"
>
> func main() {
> fmt.Printf("hello, world\n")
> }
> EOF
$ go build
$ ls
hello hello.go
$ ./hello
hello, world
$ go run hello.go
hello, world
As an unprivileged user:
$ cd $HOME && pwd
/usr/src/libcap
$ mkdir -pv $HOME/go/src/hello
/bin/mkdir: created directory 'go'
/bin/mkdir: created directory 'go/src'
/bin/mkdir: created directory 'go/src/hello'
$ cd go/src/hello
$ cat > hello.go << EOF
> package main
>
> import "fmt"
>
> func main() {
> fmt.Printf("hello, world\n")
> }
> EOF
$ go build
can't load package: package libcap/go/src/hello: cannot find package "libcap/go/src/hello" in any of:
/usr/src/libcap/go/src/hello (from $GOROOT)
/usr/src/libcap/go/src/libcap/go/src/hello (from $GOPATH)
$ ls
hello.go
$ go run hello.go
go run: cannot run non-main package
System information:
$ go version
go version go1.12.2 gccgo (GCC) 9.2.0 linux/amd64
I'm running in an LFS chroot environment and first discovered the issue while compiling the libcap package, which contains a go module, in chapter 6.28 of the 20200119-systemd book. The host is a Debian live cd:
$ uname -a
Linux debian 4.19.0-6-amd64 #1 SMP Debian 4.19.67-2+deb10u1 (2019-09-20) x86_64 GNU/Linux
The go tests appear to be fairly nominal, with very few unexpected failures (command executed from gcc build directory):
$ ../contrib/test_summary | grep -A7 Summ > log
$ less log
...
=== go Summary ===
# of expected passes 7349
# of unexpected failures 1
# of expected failures 1
# of untested testcases 6
# of unsupported tests 1
/usr/src/gcc/gcc-9.2.0/build/gcc/gccgo version 9.2.0 (GCC)
--
...
=== gotools Summary ===
# of expected passes 216
# of unexpected failures 4
# of untested testcases 159
/usr/src/gcc/gcc-9.2.0/build/./gcc/gccgo version 9.2.0 (GCC)
--
...
=== libgo Summary ===
# of expected passes 178
# of unexpected failures 6
/usr/src/gcc/gcc-9.2.0/build/./gcc/gccgo version 9.2.0 (GCC)
--
=== libgomp Summary ===
# of expected passes 6458
# of expected failures 2
# of unsupported tests 349
/usr/src/gcc/gcc-9.2.0/build/./gcc/gccgo version 9.2.0 (GCC)
--
...
Compiling under ubuntu
Linux ip 3.13.0-48-generic #80-Ubuntu SMP Thu Mar 12 11:16:15 UTC 2015
x86_64 x86_64 x86_64 GNU/Linux
with golang version
go version go1.8.3 linux/amd64
The files I have:
daemon_darwin.go daemon_freebsd.go daemon.go daemon_linux.go
daemon_windows.go LICENSE os.go README.md
The command I'm executing
GOOS=darwin GOARCH=amd64 go build -x
The output I'm getting
WORK=/tmp/go-build026556308 mkdir -p
$WORK/github.com/VividCortex/godaemon/_obj/ mkdir -p
$WORK/github.com/VividCortex/ cd
/root/go/src/github.com/VividCortex/godaemon
/home/jenkinsBuilder/tools/org.jenkinsci.plugins.golang.GolangInstallation/Go_1.8/pkg/tool/linux_amd64/compile
-o $WORK/github.com/VividCortex/godaemon.a -trimpath $WORK -p github.com/VividCortex/godaemon -complete -buildid
398bd35cdb7ed0aa42a3e6ea64677925441c29cb -D
_/root/go/src/github.com/VividCortex/godaemon -I $WORK -pack ./daemon.go ./os.go
# github.com/VividCortex/godaemon ./daemon.go:167: undefined: GetExecutablePath
we can see that daemon_darwin.go is not being included in the compile command, although I've added GOOS=darwin
If I try using GOOS=windows, it builds correctly, and daemon_windows.go is being added
I have the following make file:
default: builddocker
setup:
go get github.com/golang/protobuf/proto
go get github.com/golang/protobuf/protoc-gen-go
go get golang.org/x/net/context
go get google.golang.org/grpc
buildgo:
CGO_ENABLED=0 GOOS=linux go build -ldflags "-s" -a -installsuffix cgo -o main ./customer-server/
builddocker:
docker build -t johnwesonga/grpc/customer-server -f ./Dockerfile.build .
docker run -t johnwesonga/grpc/customer-server /bin/true
docker cp `docker ps -q -n=1`:/main .
chmod 755 ./main
docker build --rm=true --tag=johnwesonga/grpc/customer-server -f Dockerfile.static .
My Dockerfile.build has:
FROM golang
ADD Makefile /
WORKDIR /
RUN make setup
RUN make buildgo
CMD ["/bin/bash"]
But every time I run "make builddocker" I get the following error:
CGO_ENABLED=0 GOOS=linux go build -ldflags "-s" -a -installsuffix cgo -o main ./customer-server/
can't load package: package ./customer-server: open /customer-server: no such file or directory
make: *** [buildgo] Error 1
Makefile:10: recipe for target 'buildgo' failed
The command '/bin/sh -c make buildgo' returned a non-zero code: 2
make: *** [builddocker] Error 2
But if I run "Make buildgo" it works just fine, any idea why this is failing?
FYI my source tree as follows:
/go/src/github.com/johnwesonga/grpc/MakeFile
/go/src/github.com/johnwesonga/grpc/customer-server
/go/src/github.com/johnwesonga/grpc/customer-server/main.go