Byte-Cats / microman

Minimal Go Backend Starter Kit without frameworks
MIT License
1 stars 1 forks source link

new function in `applogic/json.go` to call within any `microman/handler` has a parameter of **any** go struct which returns to formatted json #18

Closed 4cecoder closed 1 year ago

4cecoder commented 1 year ago

this one is actually pretty simple!

4cecoder commented 1 year ago

Code example!

package main

import (
    "fmt"
    "encoding/json"
)

type User struct {
    Name string
}

func main() {

// created the user object
    user := &User{Name: "Frank"}

// turning the object into json
    b, err := json.Marshal(user)
    if err != nil {
        fmt.Println(err)
        return
    }

// write to Http response instead of printing line
    fmt.Println(string(b))
}

Output:

{"Name":"Frank"}