Adding gokogiri dependency causes `Killed: 9` exit - go

The environment I am using is go1.8 on MacOS Sierra.
The code:
package main
import (
"fmt"
"io/ioutil"
"github.com/moovweb/gokogiri"
"github.com/moovweb/gokogiri/xpath"
)
func main() {
fmt.Println("hello world")
b, _ := ioutil.ReadFile("x.xml")
fmt.Println(string(b))
doc, _ := gokogiri.ParseXml(b)
compiled := xpath.Compile("/path/to/node")
ss, _ := doc.Root().Search(compiled)
for _, s := range ss {
fmt.Println(s.Content())
}
}
After I build and run:
$ ./hello-world
Killed: 9
Even the hello world message is not printed. Later on, on investigating the gokogiri README, I saw instructions on installing libxml2. So I did brew install libxml2 and tried, and that also did not fix the issue.

According to similar issue, and also in Golang issue #19734, cgo command broken on darwin after performing c tool-chain (Xcode 8.3) update from Apple.
The solution: upgrade to go1.8.1 or above or add -ldflags=-s to build or test command, e.g. go build -ldflags=-s.

Related

Build constraints exclude all Go files (macOS with Go 1.18.3) [duplicate]

This question already has an answer here:
golang build constraints exclude all Go files in
(1 answer)
Closed 8 months ago.
I am new to Go and terratest. I have the following terratest
package main
import (
"regexp"
"testing"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
)
func TestS3Creation(t *testing.T) {
t.Parallel()
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
TerraformDir: "./unit-test",
})
defer terraform.Destroy(t, terraformOptions)
terraform.InitAndApply(t, terraformOptions)
bastionSecurityGroup := terraform.Output(t, terraformOptions, "bastion_security_group")
assert.Regexp(t, regexp.MustCompile(`^sg-*`), bastionSecurityGroup)
}
I initialised it as follows:
go mod init github.com/myorg/terraform-bastion-linux
When trying to run it with go test -v I get the error:
package github.com/myorg/terraform-bastion-linux: build constraints exclude all Go files in /Users/george/terraform-bastion-linux/test
My environment is as follows:
macOS Big Sur 11.6.4
CPU: Intel i9
terraform --version
Terraform v1.2.3
on darwin_amd64
go version
go version go1.18.3 darwin/amd64
I have no env variables set that start with GO, for example, env|grep GO returns nothing as result.
As advised in:
build constraints exclude all Go files in
build constraints exclude all Go files
I have tried adding the following on top of the file
//+build darwin,cgo linux
//go:build (darwin && cgo) || linux
And also exporting the GOOS and GOARCH env variables
export GOOS=darwin
export GOARCH=amd64
But I still get the same error.
How to troubleshoot this issue?
What can I try in order to run the test successfully?
As commented by JimB, the problem was that the filename included the word linux, which Go interpreted as a build constraint to only run on Linux and not Darwin. I am new to Go, and it appears to be terribly misleading with such errors. I didn't even include the file name in my original question as I thought it would be irrelevant.

no Android NDK found in $ANDROID_HOME/ndk-bundle nor in $ANDROID_NDK_HOME in Ubuntu 18.04

I'm trying to compile native GO code for android with gomobile. Looks like gomobile is not finding ndk-build.
gomobile build -target android hello.go
gomobile: no Android NDK found in $ANDROID_HOME/ndk-bundle nor in $ANDROID_NDK_HOME
I've installed Android NDK in the ANDROID_HOME directory, changed permission on the folder to 777 and added an export entry in ~/.profile as below.
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/root/work/bin:/root/ANDROID_HOME/android-ndk-r21/ndk-build
I installed gomobile from "https://godoc.org/golang.org/x/mobile/cmd/gomobile" as:
go get golang.org/x/mobile/cmd/gomobile
"gomobile init" executes without issue; however "gomobile bind" gives the same error mesage as the gomobile build error.
I've even tried to set the env variable in the "/work/src/golang.org/x/mobile/cmd/gomobile/env.go" file as below, yet the os.GetEnv call returns blank.
os.Setenv("ANDROID_HOME", "/home/usr/ANDROID_HOME/ndk-bundle/android-ndk-r21/ndk-build")
androidHome := os.Getenv("ANDROID_HOME")
if androidHome != "" {
ndkRoot := filepath.Join(androidHome, "ndk-bundle")
_, err := os.Stat(ndkRoot)
if err == nil {
return ndkRoot, nil
}
}
The build fails no matter I run it in Ubuntu 18.04 vm or a docker container. Appreciate input and feedback.
Thanks for reading.
What worked for me when facing a similar issue was to.
Make sure the ndk-bundle folder exists in
/Users/username/Library/Android/sdk
run this
export ANDROID_NDK_HOME=/Users/username/Library/Android/sdk
run your
gomobile build -target android hello.go

Verify FIPS mode in golang boringssl

how to verify if fips mode is enabled for binary in golang dev boring crypto branch ?
I dont see an easy way apart from internal golang tests
From this file:
https://go.googlesource.com/go/+/dev.boringcrypto/src/crypto/tls/fipsonly/fipsonly.go
// Package fipsonly restricts all TLS configuration to FIPS-approved settings.
//
// The effect is triggered by importing the package anywhere in a program, as in:
//
// import _ "crypto/tls/fipsonly"
//
// This package only exists in the dev.boringcrypto branch of Go.
By including that import statement in your program, it will only compile if you're using the dev.boringcrypto branch.
Here's a test main.go:
package main
import (
"fmt"
_ "crypto/tls/fipsonly"
)
func main() {
fmt.Println("Hello FIPS")
}
Using the dev.boringcrypto branch of Go:
$ go version
go version go1.12.9b4 linux/amd64
$ go run main.go
Hello FIPS
Using the normal release of Go:
$ go version
go version go1.12.9 darwin/amd64
$ go run main.go
main.go:4:2: cannot find package "crypto/tls/fipsonly" in any of:
/Users/ray/.gimme/versions/go1.12.9.darwin.amd64/src/crypto/tls/fipsonly (from $GOROOT)
/Users/ray/go/src/crypto/tls/fipsonly (from $GOPATH)

"cannot find package" error on godoc playable examples

I'm trying to make palyable example on godoc.
OS X Yosemite 10.10.5
Go: go1.7.4 darwin/amd64
test code:
$GOPATH/src/hoge/hoge_test.go
package hoge_test
import (
"fmt"
"hoge"
"testing"
)
func ExampleHoge() {
fmt.Println(hoge.Hoge())
// Output:
// hoge!!
}
The test passes:
$ go test hoge
ok hoge 0.011s
Godoc
$ godoc -play -http=:8080
I can see the hoge package's example playground as I expected on a web browser, but an error below occurs when I 'Run' the example.
tmp/sandbox389904218/main.go:5:2: cannot find package "hoge" in any of:
/usr/local/go/src/hoge (from $GOROOT)
/go/src/hoge (from $GOPATH)
I set my own GOPATH, but it's not /go as shown in the error.
What I need to run examples.
Additional
I've got a reason of the error above.
Godoc playable example redirects to play.golang.org to run the code, so the GOPATH in the error seems to be on the play.golang.org environment.
I'm still not sure how to run my own pkg example though...
This is because godoc uses the golang.org/x/tools/playground tool, which just proxies requests to golang.org. It does not appear that you can run your example functions unless they are in a package that is published on golang.org. Here's the relevant file in github: https://github.com/golang/tools/blob/master/playground/common.go which is referenced by the godoc tool here: https://github.com/golang/tools/blob/master/cmd/godoc/play.go

syscall variables undefined

When trying to build the following program on my Mac I get a build error: undefined: syscall.TCPInfo even though that variable is clearly documented http://golang.org/pkg/syscall/#TCPInfo
package main
import "syscall"
func main() {
_ = syscall.TCPInfo{}
}
Here is my go tool version.
$ go version
go version go1.3 darwin/amd64
I thought this might be an issue a lack of OS support so I tried it on http://play.golang.org, but it looks like many documented variables are just randomly missing from the syscall package: http://play.golang.org/p/w3Uk6NaZVy
Am I missing something?
The variable specified inside syscall are OS dependent.
you can add a suffix to the file to specify which os they should be built for:
// +build linux,386 darwin,!cgo
So you can use specific syscall flags for each OS.

Resources