Unlock the Full Potential of Cost-Effective Cloud Infrastructure
Elevate your cloud operations with GoCroot, the ultimate solution for businesses and developers seeking high performance and cost-efficiency. Designed to optimize backend infrastructure, GoCroot streamlines resource management and orchestration, ensuring seamless scalability and reduced overhead.
🚀 Ready to scale efficiently? Try GoCroot today!
We offer two production modes tailored for different platforms:
In Go, data structures are defined as struct
. Here’s an example:
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"`
}
json
: Used for JSON payloads in REST APIs.bson
: MongoDB field name.query
: URL query parameters, e.g., https://domain.com/?messages=example
.url
: URL generator for queries.reqHeader
: HTTP headers, e.g., Authorization
, Token
.Quick struct declaration:
var request struct {
Token string `json:"token"`
}
Define routes in a simple switch
statement. Example:
With URL Parameter
case method == "POST" && at.URLParam(path, "/webhook/nomor/:nomorwa"):
controller.PostInboxNomor(w, r)
Without URL Parameter
case method == "GET" && path == "/data/phone/all":
controller.GetBotList(w, r)
Controllers handle API logic. Below is a sample for Net HTTP and Fiber:
func Homepage(w http.ResponseWriter, r *http.Request) {
// Your logic here
}
var tasklists []report.TaskList
err := json.NewDecoder(r.Body).Decode(&tasklists)
secret := r.Header.Get("Login")
login := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]
payload, err := watoken.Decode(config.PublicKeyWhatsAuth, at.GetLoginFromHeader(r))
Example, err := atdb.GetOneDoc[model.Userdomyikado](config.Mongoconn, "user", primitive.M{"phonenumber": payload.Id})
id := r.URL.Query().Get("id")
_, header, err := r.FormFile("image")
Return JSON Response:
at.WriteJSON(w, http.StatusOK, Example)
func Homepage(ctx *fiber.Ctx) error {
// Your logic here
return ctx.Status(fiber.StatusOK).JSON(fiber.Map{"message": "success"})
}
var userreq models.UserReq
err := ctx.BodyParser(&userreq)
var h models.Header
err := ctx.ReqHeaderParser(&h)
login := ctx.Params("login")
p := new(Example)
err := ctx.QueryParser(p)
file, err := ctx.FormFile("image")
Return JSON Response:
return ctx.Status(fiber.StatusOK).JSON(fiber.Map{"status": "success"})
Easily log or manage documents in MongoDB:
document := bson.D{
{Key: "username", Value: "user1"},
{Key: "createdAt", Value: time.Now()},
}
ObjectID
:primitive.ObjectID
:
objectId, err := primitive.ObjectIDFromHex(strid)
ObjectID
to String:
strid := objectId.Hex()
Ensure code quality with Go’s built-in testing tools.
main_test.go
func TestSum(t *testing.T) {
result := Sum(2, 3)
if result != 5 {
t.Errorf("Expected 5, got %d", result)
}
}
go test -cover
go test -coverprofile=coverage.out
go tool cover -html=coverage.out
Set up your Go environment:
export GOPROXY=proxy.golang.org
Update dependencies and publish packages:
go get -u all
go mod tidy
git tag v0.1.0
git push origin --tags
Deploy your application following our deployment guide.