"Import error database/sql" for sql package - go

"Import error database/sql" for sql package.
I wanna use connect method on the controller.
I think need to install sql package.
controllers/gorm.go
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"github.com/revel/revel"
"myapp/app/models"
"log"
)
add database/sql and try import.
but the error has occurred.
error
Revel engine is NOT listening on.. localhost:58929
/Users/toshi/gocode/src/myapp/app/controllers/gorm.go
ERROR 16:10:50 watcher.go:270: Build detected an error
error="Go Compilation Error (in app/controllers/gorm.go:4): imported and not used: \"database/sql\""
/Users/toshi/gocode/src/myapp/app/controllers/gorm.go
ERROR 16:11:49 watcher.go:270: Build detected an error
error="Go Compilation Error (in app/controllers/gorm.go:4): imported and not used: \"database/sql\""
app path
/Users/toshi/gocode/src/myapp
sql-driver path
/Users/toshi/gocode/src/github.com/go-sql-driver/mysql
http://rabbitfoot141.hatenablog.com/entry/2019/03/05/000551
(reference)

Related

Golang following container logs undeclared name Log

I'm trying to build a container with testcontainers and output the logs to stdout.
I have followed their documentation (https://golang.testcontainers.org/features/follow_logs/) but when writing the Accept function, I get an error:
func (g *TestLogConsumer) Accept(l Log) {
g.Msgs = append(g.Msgs, string(l.Content))
}
The problem is that I didn't import a certain package, so I can't use "Log".
I get the following error:
undeclared name: Log
The question is, which package should I import. How can I make vscode import these packages automatically for me ? I have installed the go extension, but it doesn't import anything automatically.

unable to get hashicorp / vault

I need to use kmac Hashicorp's Vault implementation. However, when I run the code, the following simple import:
import (
"encoding/hex"
"fmt"
"github.com/hashicorp/vault/sdk/helper/kmac"
)
throws this error: no required module provides package github.com/hashicorp/vault/sdk/helper/kmac/kmac; to add it: go get github.com/hashicorp/vault/sdk/helper/kmac/kmac
If I run:
go get github.com/hashicorp/vault/sdk/helper/kmac/kmac
I get:
go get: module github.com/hashicorp/vault/sdk#upgrade found (v0.2.0), but does not contain package github.com/hashicorp/vault/sdk/helper/kmac/kmac
I also tested to download the latest version but I get the same error. Can I get help?

Error when instantiate chaincode

Please help me this issue, error happened when I instantiate my chaincode:
Currently, I guess the issue related to shim package, because I remove it in my utils package, instantiate successfully.
MyChainCode:
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/protos/msp"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/myproj/models"
"github.com/myproj/packages/utils"
)
APIstub shim.ChaincodeStubInterface
...
username, _ = utils.GetCurrentUser(APIstub)
...
My packages
package utils
import (
"github.com/hyperledger/fabric/core/chaincode/shim"
"golang.org/x/crypto/bcrypt"
)
func GetCurrentUser(stub shim.ChaincodeStubInterface) (string, error) {
cert, err := GetCreatorCert(stub)
return cert.Subject.CommonName, err
}
Issue:
Error: Error endorsing chaincode: rpc error: code = Unknown desc =
error starting container: Failed to generate platform-specific docker
build: Error returned from build: 2 "#
~vendor/github.com/hyperledger/fabric/vendor/github.com/docker/docker/pkg/archive
~vendor/github.com/hyperledger/fabric/vendor/github.com/docker/docker/pkg/archive/archive.go:364:5:
hdr.Format undefined (type *tar.Header has no field or method Format)
~vendor/github.com/hyperledger/fabric/vendor/github.com/docker/docker/pkg/archive/archive.go:364:15:
undefined: tar.FormatPAX
~/vendor/github.com/hyperledger/fabric/vendor/github.com/docker/docker/pkg/archive/archive.go:1166:7:
hdr.Format undefined (type *tar.Header has no field or method Format)
~/vendor/github.com/hyperledger/fabric/vendor/github.com/docker/docker/pkg/archive/archive.go:1166:17:
undefined: tar.FormatPAX
Fabric ver 1.1.0
Go ver 1.9.2
I faced the same problem after upgrading fabric to 1.2.0. Fabric 1.2.0 requires go 1.10.x. So I upgraded go lang to 1.10.3 and it worked like a charm.
That can be because you have old version in some files inside your hyperledger/fabric folder.
If you are following the chaincode tutorial, you can just remove your hyperledger/fabric folder.
Regards
I had the same issue by following the documentation.
Switching to branch 'release-1.1' solves the issue.
After cloning the repository proceed with:
hyperleger$ cd fabric
hyperledger/fabric$ git fetch
hyperledger/fabric$ git checkout release-1.1

error adding golang cobra package with glide

My glide.yaml import section is
import:
- package: github.com/spf13/cobra
I have
import (
"github.com/spf13/cobra"
)
in my code.
but I am getting the error
vendor/github.com/spf13/cobra/command.go:1092: c.lflags.SortFlags undefined (type *pflag.FlagSet has no field or method SortFlags)
vendor/github.com/spf13/cobra/command.go:1092: c.Flags().SortFlags undefined (type *pflag.FlagSet has no field or method SortFlags)
vendor/github.com/spf13/cobra/command.go:1240: c.parentsPflags.SortFlags undefined (type *pflag.FlagSet has no field or method SortFlags)
couples of issues on cobra repo says that can happen if pflag package is old version but in my case don't have that package installed separately.
any idea what am I doing wrong? thanks in advance!
It was problem with glide cache not updating why old version of pflag package was getting installed. More detailed info: https://github.com/spf13/cobra/issues/420

Why would 'Open connection failed:sql: unknown driver "mssql" (forgotten import?)' happen the first time on a go build?

The first time I ran https://github.com/denisenkom/go-mssqldb/blob/master/examples/simple.go I got the error 'Open connection failed:sql: unknown driver "mssql" (forgotten import?)'
I resolved this by changing
import _ "github.com/denisenkom/go-mssqldb"
to
import "github.com/denisenkom/go-mssqldb"
Which gave a different error of 'imported and not used: "github.com/denisenkom/go-mssqldb" as mssql'.
However ... after changing back to import _ "github.com/denisenkom/go-mssqldb" and building again the first error went away.
I also had to change the main function name from 'simplemain' to 'main' for some reason before it would compile.
Why would the error 'Open connection failed:sql: unknown driver "mssql" (forgotten import?)' happen on the first build?
Intended way of use seems to be
import _ "github.com/denisenkom/go-mssqldb"
which purpose is to run package's init() functions
func init() {
sql.Register("mssql", &MssqlDriver{})
}
most likely it's a some typo cause the error.

Resources