go workshop
go mod init github.com/ksupdev/updev-go-ex-stock-api go get github.com/gin-gonic/gin
go get gorm.io/driver/sqlite
go get gorm.io/driver/mysql
go get gorm.io/gorm
go get github.com/dgrijalva/jwt-go
- get images => http://localhost:8081/images/go.png
https://gorm.io/docs/models.html
- Method
func (*gin.Context).ShouldBind(obj interface{}) error
use for binding the JSON value from http request context to Struct.
func register(c *gin.Context) {
var user model.User
if c.ShouldBind(&user) == nil {
c.JSON(http.StatusOK, gin.H{"result": "register", "data": user})
}
}
- Auto create table structure incase you don't have that table before
database.AutoMigrate(&model.User{})
database.AutoMigrate(&model.Product{})
database.AutoMigrate(&model.Transaction{})
Gorm auto create table name User => users
strconv.ParseInt(c.PostForm("stock"), 10, 64) กรณีที่ไม่สามารถ convert ได้จะ return 0 มาให้เลย
- Simple build
go build
ซึ่งจะได้ file ที่มีชื่อเดียวกับ module โดยการ build แบบนี้จะมีขนาดใหญ่มาก (14.7 mb)
go build
./updev-go-ex-stock-api
- Build and remove the junk file
go build -ldflags "-s -w"
จะได้ file ที่มีขนาดเล็กกว่าแบบแรก (11.6 mb)
go build -ldflags "-s -w"
./updev-go-ex-stock-api