Golang Clean Architecture on Module Template

Golang Cheat Sheet

Pre-requisite

Please learn first :

  1. Mongodb
  2. Golang Struct
  3. Golang function

Go Fiber Based

Net HTTP Based

Type Declaration

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"`
}
  1. json : for content/type json in RestFul
  2. bson : using in mongoDB as name of field
  3. query : use as variabel name in url query, like : https://domain.com/?messages=oaus098ji
  4. url : use as variabel name for generate url query, like : https://domain.com/?messages=oaus098ji
  5. reqHeader : use as HTTP header name in Request, like Authorization, Token, Content-Type, Origin, Login

Net HTTP Controller

This is net http controller:

func Homepage(w http.ResponseWriter, r *http.Request) {
	//Controller Catcher
		//your logic
	//Controller return
}

Controller Cather

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")

Controller return

Using Example struct above.

at.WriteJSON(w, http.StatusOK, Example)

Go Fiber Controller

This is Go Fiber Controller:

func Homepage(ctx *fiber.Ctx) error {
	//Controller Catcher
		//your logic
	//Controller return
}

Controller Catcher

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")

Controller return

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})

Mongodb

Primitive.ObjectID to string

objectId, err := primitive.ObjectIDFromHex(rating.ID)

string to primitive.ObjectID

id:=objectId.Hex()

Publish Package

Commit all of your work.
Set ENV variabel in your OS : GOPROXY=proxy.golang.org
image

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

Deploy Go

Go to deploy page