VSCode import problem with windows API call - go

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

Related

Golang: generating core failed: unable to load github.com

I installed golang and started with this tutorial: https://www.howtographql.com/graphql-go/1-getting-started/
When I run:
go run github.com/99designs/gqlgen generate
I get:
reloading module info
generating core failed: unable to load github.com/my-user/hackernews/graph/model - make sure you're using an import path to a package that exists
exit status 1
What is wrong with my setup?
This is my gopath
/Users/my-pc-user/go
TLDR
$ cd your_project_path/
$ print '// +build tools\npackage tools\nimport _ "github.com/99designs/gqlgen"' | gofmt > ./tools.go
$ echo 'package model' | gofmt > ./graph/model/doc.go
$ go get .
Explanation
According to a quick start guide, you should create a package with generated code that actually is already imported by your server:
package main
import (
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"your_module_name/graph"
"your_module_name/graph/generated"
)
Because of the fact that your_module_name/graph/generated has no *.go files you cannot start a server and if you try you will get an error like:
graph/schema.resolvers.go:10:2: no required module provides package your_module_name/graph/generated; to add it:
To generate that package you will need to execute go run github.com/99designs/gqlgen generate, but there is another problem: gqlgen generates code that uses another package that is still doesn't exist yet, i.e. your_module_name/graph/model.
Additional step adding build constraints is required to not drop indirect dependency while generation process. That's why there is the first step with underscore import.
And if you put any *.go file to that directory with package directive — everything should works now:
$ cd your_project_path/
$ print '// +build tools\npackage tools\nimport _ "github.com/99designs/gqlgen"' | gofmt > ./tools.go
$ echo 'package model' | gofmt > ./graph/model/doc.go
$ go get .

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.

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