Skip to content

Instantly share code, notes, and snippets.

@sago35
Created March 25, 2023 06:00
Show Gist options
  • Save sago35/ba591a798dec789eb9450104ccd529ec to your computer and use it in GitHub Desktop.
Save sago35/ba591a798dec789eb9450104ccd529ec to your computer and use it in GitHub Desktop.
tinygo with encoding/json
package main
import (
"encoding/json"
"fmt"
"log"
)
func main() {
err := run()
if err != nil {
log.Fatal(err)
}
}
// https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/overview
const twitterJSON = `
{
"created_at": "Thu Apr 06 15:24:15 +0000 2017",
"id_str": "850006245121695744",
"text": "1\/ Today we\u2019re sharing our vision for the future of the Twitter API platform!\nhttps:\/\/t.co\/XweGngmxlP",
"user": {
"id": 2244994945,
"name": "Twitter Dev",
"screen_name": "TwitterDev",
"location": "Internet",
"url": "https:\/\/dev.twitter.com\/",
"description": "Your official source for Twitter Platform news, updates & events. Need technical help? Visit https:\/\/twittercommunity.com\/ \u2328\ufe0f #TapIntoTwitter"
},
"place": {
},
"entities": {
"hashtags": [
],
"urls": [
{
"url": "https:\/\/t.co\/XweGngmxlP",
"unwound": {
"url": "https:\/\/cards.twitter.com\/cards\/18ce53wgo4h\/3xo1c",
"title": "Building the Future of the Twitter API Platform"
}
}
],
"user_mentions": [
]
}
}
`
func run() error {
v := AutoGenerated{}
err := json.Unmarshal([]byte(twitterJSON), &v)
if err != nil {
return err
}
b2, err := json.Marshal(v)
if err != nil {
return err
}
fmt.Printf("%s\n", string(b2))
return nil
}
// https://mholt.github.io/json-to-go/
type AutoGenerated struct {
CreatedAt string `json:"created_at"`
IDStr string `json:"id_str"`
Text string `json:"text"`
User User `json:"user"`
Place Place `json:"place"`
Entities Entities `json:"entities"`
}
type User struct {
ID int64 `json:"id"`
Name string `json:"name"`
ScreenName string `json:"screen_name"`
Location string `json:"location"`
URL string `json:"url"`
Description string `json:"description"`
}
type Place struct {
}
type Unwound struct {
URL string `json:"url"`
Title string `json:"title"`
}
type Urls struct {
URL string `json:"url"`
Unwound Unwound `json:"unwound"`
}
type Entities struct {
Hashtags []any `json:"hashtags"`
Urls []Urls `json:"urls"`
UserMentions []any `json:"user_mentions"`
}
@sago35
Copy link
Author

sago35 commented Mar 25, 2023

@sago35
Copy link
Author

sago35 commented Mar 25, 2023

$ go run .
{"created_at":"Thu Apr 06 15:24:15 +0000 2017","id_str":"850006245121695744","text":"1/ Today we’re sharing our vision for the future of the Twitter API platform!\nhttps://t.co/XweGngmxlP","user":{"id":2244994945,"name":"Twitter Dev","scr
een_name":"TwitterDev","location":"Internet","url":"https://dev.twitter.com/","description":"Your official source for Twitter Platform news, updates \u0026 events. Need technical help? Visit https://twittercommunity.com/ ⌨️ #TapIntoTwitter
"},"place":{},"entities":{"hashtags":[],"urls":[{"url":"https://t.co/XweGngmxlP","unwound":{"url":"https://cards.twitter.com/cards/18ce53wgo4h/3xo1c","title":"Building the Future of the Twitter API Platform"}}],"user_mentions":[]}}
$ tinygo run .
{"created_at":"Thu Apr 06 15:24:15 +0000 2017","id_str":"850006245121695744","text":"1/ Today we窶决e sharing our vision for the future of the Twitter API platform!\nhttps://t.co/XweGngmxlP","user":{"id":2244994945,"name":"Twitter Dev","sc
reen_name":"TwitterDev","location":"Internet","url":"https://dev.twitter.com/","description":"Your official source for Twitter Platform news, updates \u0026 events. Need technical help? Visit https://twittercommunity.com/ 竚ィ・・#TapIntoTw
itter"},"place":{},"entities":{"hashtags":[],"urls":[{"url":"https://t.co/XweGngmxlP","unwound":{"url":"https://cards.twitter.com/cards/18ce53wgo4h/3xo1c","title":"Building the Future of the Twitter API Platform"}}],"user_mentions":[]}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment