go module setting
GO111MODULE state : off, on, auto(default)
- off : build package use $GOPATH
- on : build package use project module(go.mod)
- auto : build package if project is a module(go.mod) use it, there is no module(go.mod) use $GOPATH
go env -w GO111MODULE=[state]
link : https://pkg.go.dev/cmd/go/internal/get
go get [-d] [-f] [-t] [-u] [-v] [-fix] [-insecure] [build flags] [packages]
link : https://entgo.io/docs/getting-started/
go mod init <project>
go get entgo.io/ent/cmd/ent
go get -d entgo.io/ent/cmd/ent
go run entgo.io/ent/cmd/ent init <schema> <schema> <>...
link : https://entgo.io/docs/schema-def
go generate ./ent
- driver add
go get github.com/go-sql-driver/mysql
go get -d github.com/go-sql-driver/mysql
- open
import (
...
_ "github.com/go-sql-driver/mysql"
)
func main() {
client, err := ent.Open("mysql", "<user>:<password>@<protocol>(<host>:<port>)/<database>?parseTime=True")
if err != nil {
log.Fatal(err)
}
defer client.Close()
}
- driver add
go get github.com/mattn/go-sqlite3
go get -d github.com/mattn/go-sqlite3
- open
import (
...
_ "github.com/mattn/go-sqlite3"
)
func main() {
client, err := ent.Open("sqlite3", "file:<file.db>?_fk=1")
if err != nil {
log.Fatal(err)
}
defer client.Close()
}
- driver add
go get github.com/lib/pq
go get -d github.com/lib/pq
- open
import (
...
_ "github.com/lib/pq"
)
func main() {
client, err := ent.Open("postgres","host=<host> port=<port> user=<user> dbname=<database> password=<password>")
if err != nil {
log.Fatal(err)
}
defer client.Close()
}