Created
February 23, 2021 09:35
-
-
Save adibenc/282d9c6c710ca558bf62cde8f98402a9 to your computer and use it in GitHub Desktop.
base json returner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
) | |
type BaseResponse struct { | |
Success bool `json:"success"` | |
Message string `json:"message"` | |
Data string `json:"data"` | |
} | |
type BaseArrResponse struct { | |
Success bool `json:"success"` | |
Message string `json:"message"` | |
Data interface{} `json:"data"` | |
} | |
type DataAlpha struct { | |
Id int `json:"id"` | |
Name string `json:"name"` | |
D1 string `json:"d1"` | |
D2 string `json:"d2"` | |
} | |
func main() { | |
All() | |
} | |
func All() { | |
// data := BaseResponse { | |
// Success: true, | |
// Message: "msg", | |
// Data: "", | |
// } | |
data := DataAlpha { | |
Id: 1, | |
Name: "aa", | |
D1: "bb", | |
D2: "cc", | |
} | |
fmt.Println(ToJson(data)) | |
arr := make([]*DataAlpha, 0) | |
// arr := make([]*BaseResponse, 0) | |
for i := 0; i < 10; i++ { | |
// da := new(BaseResponse) | |
arr = append(arr, &data) | |
} | |
ret := BaseArrResponse {} | |
ret.Data = arr | |
// fmt.Printf("%v",arr) | |
fmt.Println(ToJson(data)) | |
fmt.Println("ToJson(ret)") | |
fmt.Println(ToJson(ret)) | |
} | |
func ToJson(data interface {}) string { | |
ret, err := json.Marshal(data) | |
if err != nil { | |
fmt.Println("err") | |
fmt.Println(err.Error()) | |
} | |
return string(ret) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment