Syscall Constant syscall.ENONET Undefined in Go - macos

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
}

Related

A nil pointer panic occurs only in debug mode while calling fmt.Sprintf

What version of Go are you using (go version)?
$ go version
go version go1.18 darwin/amd64
What did you do?
I wrote some simple code below that it try to print a struct
import v1 "k8s.io/api/core/v1"
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
func main() {
data := v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
DeletionTimestamp: nil,
},
}
str := fmt.Sprintf("%#v", data)
fmt.Println(str)
}
What did you expect to see?
same behavior with or without debugger
What did you see instead?
I was intended to print a struct called Namespace which is defined in k8s.io/api/core/v1/types.go, when I run it simply in GoLand(GoLand 2021.3.4) without debug, it runs as expected and the structure of the object had been printed in the console, but when i run it also in Goland but with debug, the program had been interrupted by an error: "bad access: nil dereference". the call stack is:
<autogenerated>:2
fmt.(*pp).handleMethods (print.go:603) fmt
fmt.(*pp).printValue (print.go:723) fmt
fmt.(*pp).printValue (print.go:806) fmt
fmt.(*pp).printValue (print.go:806) fmt
fmt.(*pp).printArg (print.go:712) fmt
fmt.(*pp).doPrintf (print.go:1026) fmt
fmt.Sprintf (print.go:219) fmt
main.main (main.go:97) main
runtime.main (proc.go:250) runtime
runtime.goexit (asm_amd64.s:1571) runtime
- Async Stack Trace
<autogenerated>:2
this is a little bit weird: If this(panic) is a intented behavior, then how can it runs correctly without debugger?
I had the same problem. I tried to upgrade the mac os version, but it did not work.
The main reason is debugserver version,mine before version is:
/Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Versions/A/Resources/debugserver --version
debugserver-#(#)PROGRAM:LLDB PROJECT:lldb-1205.0.27
and, later upgrade debugserver by:
sudo rm -rf /Library/Developer/CommandLineTools
and it will remain to intall again, and the latest version is:
debugserver-#(#)PROGRAM:LLDB PROJECT:lldb-1316.0.9.46
then, everything works.

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

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.

How can I compile a Go program?

I got Go to compile:
0 known bugs; 0 unexpected bugs
and typed in the "hello world":
package main
import "fmt"
func main() {
fmt.Printf("Hello, 世界\n")
}
Then I tried to compile it, but it wouldn't go:
$ 8c gotest2
gotest2:1 not a function
gotest2:1 syntax error, last name: main
This is going on on Ubuntu Linux on Pentium. Go installed and passed its tests. So where did I go wrong? Can someone tell me where to go from here?
I also tried this program:
package main
import fmt "fmt" // Package implementing formatted I/O.
func main() {
fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n");
}
But this was also no go (must stop making go puns):
$ 8c gotest3.go
gotest3.go:1 not a function
gotest3.go:1 syntax error, last name: main
For Go 1.0+ the correct build command is now: go build
You're using 8c, which is the c compiler. 8g will compile go, and 8l will link.
(Update for Go1.0.x)
The section "Compile packages and dependencies" now list go build as the way to compile in go.
You still call 8g behind the scene, and the parameters you could pass to 8g are now passed with -gcflags.
-gcflags 'arg list'
arguments to pass on each 5g, 6g, or 8g compiler invocation
use go run to run the go program. Here is the output.
$ cat testgo.go
package main
import "fmt"
func main() {
fmt.Printf("Hello, 世界\n")
}
$go run testgo.go
Hello, 世界
To compile Go code, use the following commands:
go tool compile gotest3.go # To create an object file.
go tool link -o gotest3 gotest3.o # To compile from the object file.
chmod +x gotest3 # To apply executable flag.
./gotest3 # To run the binary.

Resources