Unmarshal dynamic JSON based on a type key

https://play.golang.com/p/BPWVd0WAfqR package main import ( "encoding/json" "fmt" ) var bodyA = []byte(`{ "type": "A", "data": { "name": "Johnny" } }`) var bodyB = []byte(`{ "type": "B", "data": { "nickname": "J." } }`) type TypeA struct { Name string `json:"name"` } type TypeB struct { Nickname string `json:"nickname"` } func main() { req := struct { Type string `json:"type"` Data any `json:"data"` }{} err := json.Unmarshal(bodyA, &req) // bodyB if err != nil { panic(err) } switch req.Type { case "A": req.Data = new(TypeA) case "B": req.Data = new(TypeB) } err = json.Unmarshal(bodyA, &req) // bodyB if err != nil { panic(err) } message, _ := json.Marshal(&req) fmt.Println(string(message)) }

2024-05-06 · 1 min · 108 words · Me

insomnia

Auth Bearer Enviornment { "token": "Bearer **Response -> Body Attribute**", } At Bearer -> Token -> “type Response, wait then” Attribute: choose Body Attribute with $.access_token Request:choose real login url Filter (JSONPath or XPath):$.access_token More detail need to see other website tech

2024-04-23 · 1 min · 42 words · Me

Golang Concurrency

https://notes.shichao.io/gopl/ch9/

2024-04-16 · 1 min · word · Me

Two dimensional Array unique

$goods = [ 1 => [ 'id' => 12, 'price' => 77, ], 2 => [ 'id' => 43, 'price' => 855, ], 4 => [ 'id' => 34, 'price' => 1, ], ]; $goods_unique_ids_keys = array_keys(array_unique(array_column($goods, 'id'))); $goods_filter_datas = array_filter($goods, fn($key) => in_array($key, $goods_unique_ids_keys), ARRAY_FILTER_USE_KEY);

2024-02-20 · 1 min · 46 words · Me

Livewire form and outside form with button action maybe different your think

In liveiwre word some button action and alpine @click action be clicked, livewire 3 update (ajax) is different your think. Component public $test_input=1; public function tt() { info('tt'); info($this->test_input); } blade <div class="row row-cols-1 row-cols-md-3 g-4" x-data> <form wire:submit="tt"> <input type="text" wire:model='test_input'> <button type="submit">form type=submit</button> {{-- // update (ajax) --}} <button type="button">form type=button</button> {{-- // No update (ajax) --}} <button wire:click='tt; $wire.test_input=3;'>form wire:click</button> {{-- // update (ajax) and run twice --}} <button @click='$wire.tt; $wire.test_input=3;'>form @click</button> {{-- // update (ajax) and run twice --}} </form> <button type="submit">out form type=submit</button> {{-- // No update (ajax) --}} <button type="button">out form type=button</button> {{-- // No update (ajax) --}} <button wire:click='tt; $wire.test_input=3;'>out form wire:click</button> {{-- // update (ajax) --}} <button @click='$wire.tt; $wire.test_input=3;'>out form @click</button> {{-- // update (ajax) --}} </div> So be careful and try to understand ...

2023-12-31 · 1 min · 147 words · Me