Compile error when using Echo - go

I made a simple work using github.com/labstack/echo package while running I got this error.
github.com\labstack\echo\echo.go:624: e.AutoTLSManager.HTTPHandler
undefined (type autocert.Manager has no field or method HTTPHandler
package main
import "github.com/labstack/echo"
func main() {
e := echo.New()
e.Start(":")
}

This issue is addressed in issue #1082:
You seem to have a stale acme/autocert library. autocert.Manager DOES currently have that method: https://godoc.org/golang.org/x/crypto/acme/autocert#Manager.HTTPHandler I suggest a go get -u golang.org/x/crypto/acme/....

Related

Gin or Gorm complain about a unique column index being a syntax error, somewhat inconsistently

I took the example from Gorm's docs of how to create a unique index, which seems to be be simply adding a ,unique to the column tag when declaring a model. But when I tried to run it, it would always output the following message in the console:
(/Users/[...]/main.go:16)
[2021-06-26 13:59:20] near "unique": syntax error
While it seemed bizarre that an example directly from their docs would fail, I tried running that code in isolation, and it indeed worked fine on its own. Then, adding on more and more code from my app, it seemed to start outputting that message once Gin-Gonic was introduced and gin.Default() was called. I don't know if this is only because Go won't output the error by default, or there is some sort of a clash going on. But either way, I have also never had Gorm actually create the unique index; syntax error or not.
The minimum reproducible code is as follows, though it behaves rather inconsistently, running without any error about 1 out of 5 times:
package main
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/gin-gonic/gin"
)
type User struct {
gorm.Model
Name string `gorm:"size:40;index:idx_name,unique"`
}
func main() {
db, _ := gorm.Open("sqlite3", "test.db")
db.AutoMigrate(&User{})
r := gin.Default()
r.Run(":8082")
}
How would I go about fixing this; Both getting rid of the inconsistent error, and having the unique index actually being created?
If relevant, I'm running this on a Mac.
You took an example from the gorm.io but you didn't use the right packages imports.
See here the installation here: https://gorm.io/docs/#Install
You are using imports from v1 (http://v1.gorm.io/docs/) and coding with examples from the latest version. (http://gorm.io/docs/)
Look the import and the database drive initialization in the code below:
package main
import (
"github.com/gin-gonic/gin"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type User struct {
gorm.Model
Name string `gorm:"size:40;index:idx_name,unique"`
}
func main() {
db, _ := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
db.AutoMigrate(&User{})
r := gin.Default()
r.Run("localhost:8082")
}

I can't import "cloud.google.com/go/datastore"

I can't understand why this :/
I tried go get -u ** with every url that I found.
Thanks
Golang:
$ go version
go version go1.13.3 windows/amd64
Source test:
package main
import (
"fmt"
"cloud.google.com/go/datastore"
)
var client *datastore.Client
func main() {
fmt.Println("Work")
}
Error:
$ go run main.go
# google.golang.org/grpc/internal/transport
..\..\..\..\google.golang.org\grpc\internal\transport\http_util.go:270:23: cannot use hf (type "vendor/golang.org/x/net/http2/hpack".HeaderField) as type
"golang.org/x/net/http2/hpack".HeaderField in argument to d.processHeaderField
..\..\..\..\google.golang.org\grpc\internal\transport\http_util.go:675:23: cannot use "golang.org/x/net/http2/hpack".NewDecoder(http2InitHeaderTableSize,
nil) (type *"golang.org/x/net/http2/hpack".Decoder) as type *"vendor/golang.org/x/net/http2/hpack".Decoder in assignment
Go requires you make use of any package that you import. In this case you are importing "cloud.google.com/go/datastore" but not doing anything with it. The global variable that you declared is also not being used. Since it seems you are just trying to test, so I would recommend you do something with it (atleast print it). Like-
package main
import (
"fmt"
"cloud.google.com/go/datastore"
)
var client *datastore.Client
func main() {
fmt.Println(client)
}

How to import a method from an external package in Golang?

Sorry if this question is a bit basic however I have not been able to find any documentation on it.
I am trying to import the following method from example.com/User/project/controllers package
func (env *Env) Index(ctx *fasthttp.RequestCtx, ps fasthttprouter.Params){
fmt.Fprintf(ctx, "Hi there! RequestURI is %q", ctx.RequestURI())
}
Into the following file to be used in a router as follows
db, err := db.Conn()
if err != nil {
log.Panic(err)
}
env := &Env{db}
...
router.GET("/", env.controllers.Index)///this import is not valid
I have tried to use controllers.env.Index env.controllers.Index I have also tried importing with a . before the import .etc
How would one in this instance import a method from another package whereby a struct (ENV) can be passed to it? To clarify the problem here is using method on top of the package ontop of the helper e.g. method.package.helper how would I resolve the above code so that I can pass a method to a helper from an external package
Thanks
Just to clarify, you would like to pass in your db struct to your Env struct correct?
This may help if I am understanding correctly.
Since a struct is really just a collection of fields, it's dependent on the makeup of the Env struct.
What is the error that you are receiving?

Go compiler undefined method

I am getting a compiler error "w.Write undefined (type rest.ResponseWriter has no field or method Write)"
I created a bare bones test file and have the same problem:
package server
import (
"github.com/ant0ine/go-json-rest/rest"
)
func WriteTest(w rest.ResponseWriter) {
var bs []byte
w.Write(bs)
}
The method that the compiler says is not defined is definitely in the rest package.
The rest.ReponseWriter type has no Write, it has the following methods:
Header
WriteJson
EncodeJson
WriteHeader
However, it says in the comments that http.ResponseWriter methods are available by type assertion. So you should be able to write the following:
package server
import (
"github.com/ant0ine/go-json-rest/rest"
"net/http"
)
func WriteTest(w rest.ResponseWriter) {
var bs []byte
w.(http.ResponseWriter).Write(bs)
}
Write is defined on responseWriter. Note the lowercase r.

Golang Package Import

I am attempting to get the following code to compile:
package main
import (
"fmt"
"code.google.com/p/go.text/unicode/norm"
)
func main() {
fmt.Println(norm.IsNormalString("ŋ̊"))
}
I have installed the unicode/norm package. I compile with the command:
go build -o ipa ipa.go
Unfortunately, I get the following error:
# command-line-arguments
./ipa.go:9: undefined: norm.IsNormalString
make: *** [ipa] Error 2
It seems that the package is being imported correctly, but I cannot access any of its members. I have tried changing the method from being called to another from norm, but I still get the error. This leads me to believe that I'm fundamentally misunderstanding something about go's package system.
func (Form) IsNormalString
func (f Form) IsNormalString(s string) bool
IsNormalString returns true if s == f(s).
IsNormalString is not a function, it's a method on type Form. For example,
package main
import (
"code.google.com/p/go.text/unicode/norm"
"fmt"
)
func main() {
fmt.Println(norm.NFC.IsNormalString("ŋ̊"))
}
Output:
true

Resources