Please learn first :
type Example struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty" query:"id" url:"_id,omitempty" reqHeader:"token"`
Messages string `json:"messages" bson:"messages" query:"messages" url:"messages" reqHeader:"token"`
}
This is net http controller:
func Homepage(w http.ResponseWriter, r *http.Request) {
//Controller Catcher
//your logic
//Controller return
}
Get JSON Body
var tasklists []report.TaskList
err := json.NewDecoder(r.Body).Decode(&tasklists)
Get Header From Client Request
secret = r.Header.Get("secret")
Get URL Param e.g: localhost/:login
login := at.GetParam(r)
//or
login := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]
Get URL Query using Example struct above e.g: localhost/?id=bagong
id := r.URL.Query().Get("id")
Get File Upload by FormFile using image as parameter name
_, header, err := r.FormFile("image")
Using Example struct above.
at.WriteJSON(w, http.StatusOK, Example)
This is Go Fiber Controller:
func Homepage(ctx *fiber.Ctx) error {
//Controller Catcher
//your logic
//Controller return
}
Get JSON Body
var userreq models.UserReq
err := ctx.BodyParser(&userreq)
Get Header From Client Request
var h models.Header
err := ctx.ReqHeaderParser(&h)
Get URL Param e.g: localhost/:login
login := ctx.Params("login")
Get URL Query using Example struct above e.g: localhost/?id=bagong
p := new(Example)
err := ctx.QueryParser(p)
Get File Upload by FormFile using image as parameter name
file, err := ctx.FormFile("image")
return status and json
return ctx.Status(fiber.StatusServiceUnavailable).JSON(fiber.Map{"error": "id tidak valid"})
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
return ctx.Status(fiber.StatusServiceUnavailable).JSON(fiber.Map{"error": "id tidak valid"})
return ctx.Status(fiber.StatusForbidden).JSON(fiber.Map{"error": "Tidak ada data laporan ditemukan"})
return ctx.Status(fiber.StatusOK).JSON(fiber.Map{"error": errstr, "update": res.ModifiedCount, "wa": resp.Response})
string to primitive.ObjectID
objectId, err := primitive.ObjectIDFromHex(strid)
Primitive.ObjectID to string
strid:=objectId.Hex()
Commit all of your work.
Set ENV variabel in your OS : GOPROXY=proxy.golang.org
go get -u all #update existing package
go mod tidy #generate go mod
git tag #check current version
git tag v0.0.3 #set tag version
git push origin --tags #push tag version to repo
go list -m github.com/aiteung/presensi@v0.0.3 #publish to pkg dev, replace ORG/URL with your repo URL
Go to deploy page