Quick actions

cmd+k|ctrl+k

Navigation

Languages

Unmarshal in go must from byte

Snippet info

Language

Go

Visibility

public

Author

ubaidillah.hf

Created

2022-03-03T15:57:32.629784Z

Updated

2022-03-03T15:57:48.188516Z

package main

import (
    "encoding/json"
    "fmt"
)

type Order struct {
	ID        string    `json:"id" gorm:"column:id;primaryKey"`
	Items     string    `json:"items" gorm:"column:items"`
	Status    uint8     `json:"status" gorm:"column:status"`
}

func (o Order) ToEntity()  {
    out := []struct {
		Name  string 
		Qty   uint8
		Price float32
	}{}
	var jsonData = []byte(o.Items)
	fmt.Println("Items", jsonData)
	if err := json.Unmarshal(jsonData, &out); err != nil {
		fmt.Println("Error", err)
	}
	fmt.Println("Out", out)
}

func main() {
    order1 := Order{
        ID: "592494d3-8c42-4881-b04a-1f9a1a63565c",
        Items: `[{"Name": "ubaidillah", "Qty": 1, "Price":10}]`,
        Status: 1,
    }
    order1.ToEntity()
}
INFO