Quick actions

cmd+k|ctrl+k

Navigation

Languages

unmarshal

Snippet info

Language

Go

Visibility

public

Author

ubaidillah.hf

Created

2022-04-14T10:17:32.198464Z

Updated

2022-04-14T10:17:32.198464Z

package main

import (
    "fmt"
    "encoding/json"
)

func main() {
    
    var jsonUnmarshal = json.Unmarshal
    
    out := struct {
		ID        uint64 `json:"id"`
		Code      string `json:"code"`
		Content   string `json:"content"`
		CreatedAt string `json:"createdAt"`
		UpdatedAt string `json:"updatedAt"`
	}{}
	
	var jsonVal interface{}
	
	jsonVal = `{
        "id": 1,
        "code": "CEK",
        "content": "CEK",
        "createdAt": "2006-01-02T15:04:05Z",
        "updatedAt": "2006-01-02T15:04:05Z"
    }`

cacheStr, _ := jsonVal.(string)
	
	
	
	err := jsonUnmarshal([]byte(cacheStr), &out)
	if err != nil{
	    fmt.Println("ERRORNYA =>", err)
	}
    fmt.Println("Hello World!", out)
}
INFO