Import issue with docker's libcontainer - go

When using the docker's libcontainer (specifically the network part), I get an undefined error while building using go build on my project.
import (
"encoding/json"
...
"github.com/docker/libcontainer/network"
)
func SetIP(a Address) (err error) {
...
err = network.SetInterfaceIp(a.Link, a.IP)
....
}
The error itself:
./addresses.go:170: undefined: network.SetInterfaceIp
I've checked inside the library itself and I can find this so called function.

I was building on OSX, which cannot be done when using libcontainer. After using a debian VM the whole project was built correctly.
Kudos to #JimB and #Not_a_Golfer for the hints.

Related

VSCode import problem with windows API call

When importing golang.org/x/sys/windows in VSCode, I can only choose SIGDescribe, nothing else.
Hovering over the import, following errors appear.
error while importing golang.org/x/sys/windows: build constraints exclude all Go files in /home/username/go/pkg/mod/golang.org/x/sys#v0.0.0-20210630005230-0f9fa26af87c/windows
could not import golang.org/x/sys/windows (no required module provides package "golang.org/x/sys/windows")compilerBrokenImport
The manual command go get golang.org/x/sys/windows gives the following error message
Command 'gopls.go_get_package' failed: Error: err: exit status 1: stderr: package golang.org/x/sys/windows: build constraints exclude all Go files in /home/username/go/pkg/mod/golang.org/x/sys#v0.0.0-20210630005230-0f9fa26af87c/windows .
I already re-installed Golang and updated GoTools in VSCode, no changes.
Goal: The following code below should work.
package main
import "golang.org/x/sys/windows"
func main() {
user32DLL := windows.NewLazyDLL("user32.dll")
}
OS: Ubuntu 21.04
GO Version: 1.16.6
Editor: VSCode 1.58.1
Make a folder somewhere something. Then make a file something/main.go:
package main
import "golang.org/x/sys/windows"
func main() {
println(windows.EVENTLOG_ERROR_TYPE == 1)
}
Then build:
go mod init something
go mod tidy
go build

Syscall Constant syscall.ENONET Undefined in Go

I tried to run the following bar.go script
package main
import (
"fmt"
"syscall"
)
func main() {
fmt.Printf("%d\n", uintptr(syscall.ENONET))
}
by calling go run bar.go and get this error:
# command-line-arguments
./bar.go:9:29: undefined: syscall.ENONET
I am using Mac and go version 1.14.3 darwin/amd64. I tried to run this exact script on Go playground, https://play.golang.org/p/ecMZPsGgGOa and it worked.
I tried to run the script using CGO_ENABLED=1 GOOS=linux GOARCH=amd64 and got this error instead:
fork/exec /var/folders/2l/dj6ph5t92y17vhtv3n6xzr5r0000gn/T/go-build847134732/b001/exe/bar: exec format error
How do I get syscall.ENONET to work on Mac?
Thank you
Yes, there's some weird problem with it. Although, syscall is locked down now.
Deprecated: this package is locked down.
Callers should use the corresponding package in the golang.org/x/sys repository instead.
I tried out with go version go1.14.3 darwin/amd64, but I got the same problem.
But in the documentation, we have:
ENONET = Errno(0x40)
As, Errno is also exported type you can explicitly mock the same behaviour:
package main
import (
"fmt"
"syscall"
)
func main() {
fmt.Printf("%d\n", syscall.Errno(0x40)) // 64
fmt.Printf("%v\n", syscall.Errno(0x40)) // host is down
}

Fyne import issue

I have just started golang coding, and I don't know how to import module. I looked on the internet, and i found that line of code to write in the cmd.
$ go get fyne.io/fyne
And now, in my disk there are some package.
Then, i wrote this code:
package main
import (
"fyne.io/fyne/app"
)
func main(){
a:= app.New()
w := a.NewWindow("Clock")
}
But when i execute it, i have that error:
exec: "gcc": executable file not found in %PATH%
Someone know where is the issue? Thanks

Having trouble getting started on go. `package main` throws run time error- index out of range?

I'm a complete beginner in go. And I just installed gophernotes and intend to use Jupyter Notebook for main programming.
This program gives the following error when run in Jupyter:
Cell1: package main
Out1: runtime error: index out of range
Cell2: import "fmt"
func main() {
fmt.Println("hello world")
}
main()
Out2: hello world
When I write the same in a test.go and execute from bash: go run test.go, I get the following:
Deepaks-MacBook-Air:JUPYTER deepak$ go run test.go
go: disabling cache (/Users/deepak/Library/Caches/go-build) due to initialization failure: open /Users/deepak/Library/Caches/go-build/log.txt: permission denied
# command-line-arguments
./test.go:6:1: syntax error: non-declaration statement outside function body
I think that having "package main" is a problem. The way Go works with Jupyter is apparently different than how Go works on its own. You don't need a package statement with Jupyter.
Also you should never call main(). That is done automatically when you run the program with go run or go build.
I am not familiar with Jupyter Notebook and how it uses Go. Maybe you do need to call the function. If that is the case do not name your function main because that is simply confusing.
From what I have seen of Jupyter / Go examples you don't need a Go function you can just list out the code.

Cross compile net/http for distribution

I have build the following code in a docker container with the following architecture:
cat /proc/version
Linux version 3.16.7-tinycore64 (root#064f0e1ce709) (gcc version 4.7.2 (Debian 4.7.2-5) ) #1 SMP Tue Dec 16 23:03:39 UTC 2014
package main
import "fmt"
func main() {
fmt.Println("Hello")
}
The binary distributed, runs with no problem on a busybox container, with the same architecture without installing golang.
The problem
When I do the same for the following code:
package main
import (
"fmt"
"net/http"
)
const (
port = ":80"
)
var calls = 0
func HelloWorld(w http.ResponseWriter, r *http.Request) {
calls++
fmt.Fprintf(w, "Hello, world! You have called me %d times.\n", calls)
}
func init() {
fmt.Printf("Started server at http://localhost%v.\n", port)
http.HandleFunc("/", HelloWorld)
http.ListenAndServe(port, nil)
}
func main() {}
Then I get:
ash: ./hello_world: not found
I might be missing some dependencies - like "net/http"?
But I thought the go build would build all into the binaries.
This is for both go build & go install.
Any idea?
The answer is most probably the one described in this article.
Some critical parts of the standard library use CGO [...] if you cross-compile Go to Darwin or Linux your programs won’t use the system DNS resolver. They also can’t use the native host certificate store. They also can’t look up the user’s home directory, either.
And CGO links against some standard system interfaces by default, dynamically.
The article suggests using gonative to fix the problem. If that's not your cup of tea, some people suggest using:
go build -ldflags "-linkmode external -extldflags -static"
Also read: https://groups.google.com/d/topic/golang-nuts/H-NTwhQVp-8/discussion
I think you need to disable cgo and build with netgo flag :
The net package requires cgo by default because the host operating
system must in general mediate network call setup. On some systems,
though, it is possible to use the network without cgo, and useful to
do so, for instance to avoid dynamic linking. The new build tag netgo
(off by default) allows the construction of a net package in pure Go
on those systems where it is possible.
The netgo tag requires version 1.2 and above.

Resources