Victor7k
(Victor Ferreira Machado)
October 23, 2023, 2:28pm
1
While writing my Golang code I ended up encountering two errors that I don’t know how to fix. Please, does anyone happen to know??
The First Error happened when I put “import C”, but the GO code I’m creating needs CGO to work so I don’t know why this error is happening or how to fix it.
The Second Error happens when I run the program. I tested and this error has no connection with the first error. I don’t understand why this error is happening or how to fix it, but I already checked the path described and the file described is there.
This is my main.go
package main
// #include "C:\src\github.com\miekg\pkcs11\pkcs11.h"
import "C"
import (
"fmt"
"github.com/miekg/pkcs11"
)
func main() {
p := pkcs11.New("aetpkss1.dll")
err := p.Initialize()
if err != nil {
panic(err)
}
defer p.Destroy()
defer p.Finalize()
slots, err := p.GetSlotList(true)
if err != nil {
panic(err)
}
session, err := p.OpenSession(slots[0], pkcs11.CKF_SERIAL_SESSION|pkcs11.CKF_RW_SESSION)
if err != nil {
panic(err)
}
defer p.CloseSession(session)
err = p.Login(session, pkcs11.CKU_USER, "1234")
if err != nil {
panic(err)
}
defer p.Logout(session)
p.DigestInit(session, []*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_SHA_1, nil)})
hash, err := p.Digest(session, []byte("this is a string"))
if err != nil {
panic(err)
}
for _, d := range hash {
fmt.Printf("%x", d)
}
fmt.Println()
}
And this is my settings.json
{
"go.buildTags": "cgo",
"go.toolsEnvVars": {
"CGO_ENABLED": "1"
} /* , "go.toolsGopath": "C:\\msys64\\ucrt64\\bin;${workspaceFolder}\\bin;${env:GOPATH}\\bin" */
}
Unfortunately I have no experience with CGO so I can’t be too much help here. But there were some hidden (which is ridiculous as they are potentially helpful) comments on the mentioned issue:
opened 12:24AM - 11 May 20 UTC
NeedsFix
GoCommand
### What version of Go are you using (`go version`)?
<pre>
$ go version
go … version go1.14.1 linux/amd64
go version devel +57e32c4fbd Sun May 10 14:43:46 2020 +0000 linux/amd64
</pre>
### What's wrong
When cgo preprocessing fails, `go list -json -compiled -e` prints an error on stderr and exits with a non-zero status, but it does not populate the Error field of the package. It is possible to detect that something went wrong because CompiledGoFiles isn't populated, but we can't tell _why_ it failed. Two common reasons are a missing C compiler, and missing libraries.
### Example output
```
$ CC=/does/not/exist go list -json -compiled -e runtime/cgo
# runtime/cgo
fork/exec /does/not/exist: no such file or directory
{
"Dir": "/nix/store/sylhlk3fd3xik0d8hqrm20zxnfc3x7m4-go-1.14.1/share/go/src/runtime/cgo",
"ImportPath": "runtime/cgo",
"Name": "cgo",
"Doc": "Package cgo contains runtime support for code generated by the cgo tool.",
"Target": "/nix/store/sylhlk3fd3xik0d8hqrm20zxnfc3x7m4-go-1.14.1/share/go/pkg/linux_amd64/runtime/cgo.a",
"Root": "/nix/store/sylhlk3fd3xik0d8hqrm20zxnfc3x7m4-go-1.14.1/share/go",
"Match": [
"runtime/cgo"
],
"Goroot": true,
"Standard": true,
"Stale": true,
"StaleReason": "build ID mismatch",
"GoFiles": [
"callbacks.go",
"callbacks_traceback.go",
"iscgo.go",
"mmap.go",
"setenv.go",
"sigaction.go"
],
"CgoFiles": [
"cgo.go"
],
"IgnoredGoFiles": [
"callbacks_aix.go",
"dragonfly.go",
"freebsd.go",
"netbsd.go",
"openbsd.go",
"signal_darwin_armx.go"
],
"CFiles": [
"gcc_context.c",
"gcc_fatalf.c",
"gcc_libinit.c",
"gcc_linux_amd64.c",
"gcc_mmap.c",
"gcc_setenv.c",
"gcc_sigaction.c",
"gcc_traceback.c",
"gcc_util.c"
],
"HFiles": [
"libcgo.h",
"libcgo_unix.h"
],
"SFiles": [
"asm_amd64.s",
"gcc_amd64.S"
],
"CgoCFLAGS": [
"-Wall",
"-Werror"
],
"CgoLDFLAGS": [
"-lpthread"
],
"Imports": [
"C",
"unsafe"
],
"Deps": [
"unsafe"
]
}
```
Related: #34229 and #25842
/cc @bcmills @matloob @heschik
Have you tried those fixes? Also - maybe back up and follow this guide and see where it gets you:
Victor7k
(Victor Ferreira Machado)
October 25, 2023, 6:22pm
3
Thank you for the help but the problem still happening