Working with APIs and Parsing JSON Data in Golang: A Quickstart Reference Powered by RapidAPI and Hacker News

Welcome back internet friend,

At the moment I’m currently doing a little hacking in Golang, and just wanted to post this quick little example of how to work with an api and parse json data in Go. Unlike some of the other languages I’ve worked with, in Golang you will need to create a struct to represent the JSON data you are receiving from the API endpoint you are hitting. That’s the trickiest part in my opinion. Also, the JSON data you will be working with is going to be an array of bytes, not a string. Last, you’ll use json.Unmarshal to parse your JSON and extract the information you are looking for.

Unfortunately, I don’t have time to write up a detailed post about how all of this works. However, I think the code snippet below is pretty cool and should provide a good reference if you happen to be playing around with APIs and JSON using Go. Note, the code below is using RapidAPI’s Hacker News endpoint to provide the JSON data. So if you’d like to try running this code yourself, make sure to visit rapidapi.com, register for an account, and replace the API key below with your own. Enjoy!

// REFERENCE (Parsing JSON in Golang): https://www.sohamkamani.com/blog/2017/10/18/parsing-json-in-golang/
// REFERENCE (Cast Int as String in Golang): https://yourbasic.org/golang/convert-int-to-string/
package main
import (
"fmt"
"net/http"
"io/ioutil"
"encoding/json"
"strconv"
)
type TopStories struct {
Story []int
}
type Story struct {
By string
Descendants int
Id int
Kids []int
Score int
Title string
Type string
URL string
}
func main() {
// ---------------------------------------------------------------------
// Hit the top stories endpoint for the Hacker News API
// via RapidAPI to get the story ids for the top stories
// today trending on Hacker News
// ---------------------------------------------------------------------
url := "https://community-hacker-news-v1.p.rapidapi.com/topstories.json?print=pretty"
// Create Request
request, _ := http.NewRequest("GET", url, nil)
request.Header.Add("x-rapidapi-host", "community-hacker-news-v1.p.rapidapi.com")
request.Header.Add("x-rapidapi-key", "YouR-SuPER-SWeeT-RaPiDaPi-KeY-GoeS-HeRe")
// Get Result, Result Body, and Result Body String
result, _ := http.DefaultClient.Do(request)
defer result.Body.Close()
resultBody, _ := ioutil.ReadAll(result.Body)
resultBodyStr := string(resultBody)
// Create an array to store our topStories,
// then append the story ids to our array
// using json.Unmarshall
var topStories []int
json.Unmarshal([]byte(resultBodyStr), &topStories)
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Next, Let's fetch some information about the top trending story on
// Hacker News. TODO: Fetch data for ALL the trending stories on HN
// ---------------------------------------------------------------------
var topStoryID string = strconv.Itoa(topStories[0])
url = "https://community-hacker-news-v1.p.rapidapi.com/item/" + topStoryID + ".json?print=pretty"
request, _ = http.NewRequest("GET", url, nil)
request.Header.Add("x-rapidapi-host", "community-hacker-news-v1.p.rapidapi.com")
request.Header.Add("x-rapidapi-key", "YouR-SuPER-SWeeT-RaPiDaPi-KeY-GoeS-HeRe")
result, _ = http.DefaultClient.Do(request)
defer result.Body.Close()
resultBody, _ = ioutil.ReadAll(result.Body)
resultBodyStr = string(resultBody)
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Now, let's parse the top story...
// ---------------------------------------------------------------------
var topStory Story
json.Unmarshal([]byte(resultBodyStr), &topStory)
fmt.Println("Top Story...")
fmt.Printf("Title: %s\n", topStory.Title)
fmt.Printf("URL: %s\n", topStory.URL)
// ---------------------------------------------------------------------
}
view raw hackernews.go hosted with ❤ by GitHub
 

Working with WebSockets in React Native // TLDR: Avoid Socket.IO

Welcome back dear internet friend!

Having trouble getting started with WebSockets in React Native? I’ve got the answer you’re looking for! After many hours of messing around with WebSockets in React Native for the first time today I’ve made some discoveries that hopefully will be of use to someone else out there on the internet.

First, avoid Socket.IO. When attempting to tackle this problem I naturally started with Socket.IO as I’ve used it before in the past, it’s sort of the #1 name in WebSocket programming, so it would make sense as a good starting point. However, I had a little trouble getting it to work. Now in all fairness to Socket.IO, their WebSocket library may not have been the problem in my setup. But along my winding path today I discovered that Socket.IO doesn’t work “out of the box” in React Native, and while it does work, it actually falls back upon HTTP Long Polling if you simply install it in your project with modification. Furthermore, the official React Native documentation specifically mentions the built-in WebSocket API that comes with JavaScript, so I would recommend using that.

The next stumbling block you might run into is setting up your own WebSocket server. For me, this ended up being a lot trickier than simply setting up a WebSocket server for a web app. I have setup WebSocket servers for both NodeJS and Flask web apps fairly easily, but doing the same with React Native gave me a lot of problems. So what I found extremely helpful was to use a test endpoint from a 3rd party that simply echos back any WebSocket messages sent from my app.

After a days work fumbling around, riding the struggle bus, I present a super simple quick start template for getting up and running with WebSockets in React Native. The endpoint URL in the code points to the 3rd party WebSocket test endpoint described above, so the solution below should work as is with no modification necessary. Just create a new React Native app, delete the contents of your App.js file, and copy & paste the code below, and you should be well on your way! (and this way, you will know your React Native app is working properly and playing nice with WebSockets before you start messing around with your server side code. Also, remember to avoid Socket.IO if you want to use the built-in WebSocket API like in my example:

EDIT: According to notable developer Miguel Grinberg, Socket.IO will not fall back upon HTTP Long Polling in React Native if you specify that you want to use [websockets] as your transport method. But never let the truth get in the way of a good story, so I’m leaving this post unedited 😉

// REFERENCE (ws.send)
// https://dev.to/finallynero/using-websockets-in-react-4fkp
// REFERENCE (official react-native docs)
// https://facebook.github.io/react-native/docs/network#websocket-support
import React from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
Button,
} from 'react-native';
var ws = new WebSocket('ws://echo.websocket.org/');
ws.onopen = () => {
alert("ws.onopen called!");
ws.send('something');
}
ws.onmessage = (e) => {
alert("ws.onmessage called!");
}
ws.onerror = (e) => {
alert("ws.onerror called!");
}
ws.onclose = (e) => {
alert("ws.onclose called!");
}
class App extends React.Component {
constructor(props) {
super(props);
}
handlePingWebSocketServer() {
ws.send("hello, world");
}
render() {
return(
<View>
<Text>hello, world</Text>
<Button
title="Ping WebSocket Server"
onPress={ () => this.handlePingWebSocketServer() } />
</View>
);
}
}
export default App;
view raw App.js hosted with ❤ by GitHub
 

Command Line User Input in Golang

Cheatsheet reference for handling command line user input in Golang:

// Command Line User Input in Golang
// REFERENCE: https://stackoverflow.com/questions/20895552/how-to-read-from-standard-input-in-the-console
package main
import (
"fmt"
"bufio"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print(">>> ")
userInput, _ := reader.ReadString('\n')
fmt.Println(userInput)
fmt.Print(">>> ")
userInput2, _ := reader.ReadString('\n')
fmt.Println(userInput2)
fmt.Print(">>> ")
userInput3, _ := reader.ReadString('\n')
fmt.Println(userInput3)
}
view raw userinput.go hosted with ❤ by GitHub
 

How to do a FULL JOIN in MySQL, TLDR: Fake It!

Over the past two days I’ve been pretty stumped working on this new database for my latest app, Moolabeast. I’ve been experimenting with different table designs etc., and discovered what I really need is a FULL JOIN. Great! But just one problem… MySQL doesn’t support FULL JOINS. Yikes!

On StackOverflow I found a bunch of different posts from developers describing how to “fake” a FULL JOIN using various SQL statements that were a little beyond my comprehension. However, I finally found an excellent blog post from TablePlus explaining how this works.

Therefore, if you find yourself struggling to do a FULL JOIN in MySQL, I suggest checking out TablePlus’s explanation. I don’t think I could do it justice! It took me a while to find a really good post on the subject, thus that’s why I wanted to write this post to point more people in the right direction.

Essentially you need to do a LEFT JOIN, and a RIGHT JOIN, and then a UNION which will merge the results of the two joins. Now it isn’t perfect, but it’s probably close enough:

SELECT * FROM a
LEFT JOIN b ON a.id = b.id
UNION
SELECT * FROM a
RIGHT JOIN b ON a.id = b.id

Nifty ven diagrams from TablePlus demonstrating the UNION of the LEFT JOIN and RIGHT JOIN results to create a FULL JOIN:

 

The React Native Equivalent of Tap Gesture Recognizer and resignFirstResponder

This blog post is brought to you by the developer of Moolabeast: Automated Expense Tracking for Android and iOS. Avoid overspending when you start tracking your expenses with Moolabeast.

Having trouble dismissing the keyboard in your React Native app on iOS? Would you like the keyboard to simply go away after a user taps somewhere else on the screen after entering some text into a TextInput? Here’s what you’re looking for: <TouchableWithoutFeedback> and Keyboard.dismiss() are the React Native equivalents of Tap Gesture Recognizer and resignFirstResponder. Simply wrap your view in a <TouchableWithoutFeedback> component and call the Keyboard.dismiss() method onPress and the keyboard will automatically dismiss itself when a user taps an area of the screen outside of the currently selected <TextInput>:

import React, { Component } from 'react';
import {
Alert,
Button,
StyleSheet,
Text,
TextInput,
Keyboard,
TouchableWithoutFeedback,
View
} from 'react-native';
class DismissKeyboardExample extends Component {
constructor(props) {
super(props);
this.state = {
emailTextInput: "",
passwordTextInput: ""
};
}
handleEmailTextInput(text) {
let previousState = this.state;
let newState = previousState;
newState.emailTextInput = text;
this.setState(newState);
}
handlePasswordTextInput(text) {
let previousState = this.state;
let newState = previousState;
newState.passwordTextInput = text;
this.setState(newState);
}
handleTestSignupForm() {
let email = this.state.emailTextInput;
let password = this.state.passwordTextInput;
let info = "email entered: " + email + "\n";
info += "password entered: " + password;
alert(info);
let previousState = this.state;
let newState = previousState;
newState.emailTextInput = "";
newState.passwordTextInput = "";
this.setState(newState);
}
render() {
return (
<TouchableWithoutFeedback
accessible={false}
onPress={ () => Keyboard.dismiss() }>
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Dismiss Keyboard Example</Text>
<TextInput
placeholder="Email"
onChangeText={ (text) => this.handleEmailTextInput(text) }
value={this.state.emailTextInput} />
<TextInput
placeholder="Password"
onChangeText={ (text) => this.handlePasswordTextInput(text) }
value={this.state.passwordTextInput} />
<Button
title="Sign Up"
onPress={ () => this.handleTestSignupForm() } />
</View>
</TouchableWithoutFeedback>
);
}
}
export default DismissKeyboardExample;
 

const is not constant

This blog post is brought to you by the developer of Moolabeast: Automated Expense Tracking for Android and iOS. Avoid overspending when you start tracking your expenses with Moolabeast.

Welcome back dear internet friend!

Quick little rant and opinion piece here tonight on the blog. I’ve been programming a lot lately in ReactJS and React-Native, following along with all of the various quickstart guides and tutorials related to the React tech stack, and have found myself thrust into this brave new world of JavaScript where variables do not exist. In this new world var is out, and const is in!

At least that’s how it appears looking at all of this “modern” JavaScript code I’ve been working with. I can’t remember the last time I saw the keyword var. But tonight I sort of had an epiphany: These constants don’t seem very constant to me. We’re using them just like variables!

To test this out, I fired up repl to see, can you just write over a constant whenever you feel like it? Or if you declare a new constant with the same name, is all forgiven? Well here’s what I found out. The JavaScript interpreter will get mad at you and crash if you try to write over a constant, or declare a new constant with the same name. But, if you declare a new constant inside a function you can write over it as many times as you like!

So… essentially this is what I’ve been seeing in all the modern JavaScript code I’ve been looking at. People are using constants just like variables, except the constants are always inside of a render() function or something similar so you can keep writing over it over and over and over again.

I’m sure most professional React developers know this. However, this seems pretty stupid to me. Why use a constant if you actually want a variable? I’m sure the answer has something to do with immutability! and functional programming! But I’m not buying it. const is not constant, and the emperor has no clothes.

const myConstant = "const stands for constant. It is immutable and does not change...";
console.log(myConstant);
function constIsNotReallyConstant() {
const myConstant = Math.random().toString();
console.log(myConstant);
}
for (var i = 0; i < 13; i++) {
constIsNotReallyConstant();
}
 

How to Setup a New Flask App on a Mac

This dev tip is brought to you by the developer of Moolabeast: Automated Expense Tracking for Android and iOS. Avoid overspending when you start tracking your expenses with Moolabeast.

Welcome back internet friend!

Quick post here tonight on how to setup a new flask app for development on a Mac. In the past I’ve done all of my Python/Flask development on a Windows desktop computer, but since I’m now doing all of my development on a Macbook Pro, I wanted to write another little quickstart reference for myself or anyone else out there on the internet to get up and running with Python and Flask on a Mac (assumes you’ve already installed Python 3, if you haven’t, please visit https://python.org):

First, make sure virtualenv is installed

$ pip3 install virtualenv

Then create a directory for your new Flask project

$ mkdir MyNewFlaskApp

Navigate to your newly created directory

$ cd MyNewFlaskApp

Create your virtual environment

$ virtualenv venv --system-site-packages

Activate the virtual environment

$ source venv/bin/activate

Make sure Flask is installed

(venv) $ pip3 install Flask

Now write the actual python code for your flask application. We’ll call our python script my_new_flask_app.py. Save this script in your MyNewFlaskApp directory.

from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'hello, world'
view raw my_new_flask_app.py hosted with ❤ by GitHub

Set the FLASK_APP system variable

(venv) $ export FLASK_APP=my_new_flask_app.py

Run Flask

(venv) $ flask run

Visit http://127.0.0.1:5000 to see your app in action

 

Learn Golang in ~30 Minutes

I’ve been working with a student of mine recently in Golang and noticed that my skills were sort of lacking! So here’s a quick little cheat sheet reference for coding in Golang. If you need any help getting your computer setup to run Golang, check out Learn Go in 12 Minutes on YouTube.

If you haven’t already installed Golang and set up your environment, do that first…

Step 1: Install Golang

Then, create your “go” directory where you will store all of your golang projects on your local machine:

$ cd $HOME

$ mkdir go

$ cd go

$ mkdir src

$ cd src

Next, create a subdirectory for your project:

$ mkdir hello

$ cd hello

Now create a file called hello.go which will contain our hello, world program:

package main

import "fmt"

func main() {
	fmt.Printf("hello, golang\n")
}

Compile & Run:

$ go build

$ ./hello

Last, check out the example below (based on Learn Golang in 12 Minutes) as a reference/cheatsheet:

// REFERENCE (Learn Golang in 12 Minutes): https://www.youtube.com/watch?v=C8LgvuEBraI
package main
import (
"fmt"
)
func main() {
// functions
fmt.Println("hello, golang!")
// variables
var x int = 4
y := 7 // use walrus tooth operator to infer type
// if statements
if (x == 5) {
fmt.Println("x equals five!")
} else if (x != 5 && y == 7) {
fmt.Println("x does not equal five, but y equals seven...")
} else {
fmt.Println("I dunno man?")
}
// arrays (fixed length)
var a [5]int
a[0] = 100
a[1] = 101
a[2] = 102
a[3] = 103
a[4] = 104
fmt.Println(a[0])
// slices (adjustable length "array")
var b []int
b = append(b, 200)
b = append(b, 201)
b = append(b, 202)
b = append(b, 203)
b = append(b, 204)
fmt.Println(b[0])
// maps (key value pairs, "dictionary")
c := make(map[string]int)
c["foo"] = 300
c["bar"] = 301
c["baz"] = 302
fmt.Println(c["foo"])
// loops
// (no while loops in golang, only for loops!)
for i:= 0; i < 5; i++ {
fmt.Println(i)
}
// if you need something like a while loop,
// this can be accomplished with a for loop
// like so...
j := 0
for j < 1 {
fmt.Println("look at me go!")
fmt.Println("just kidding... lets shut it down!")
j = 1
}
}
view raw learngo.go hosted with ❤ by GitHub

Need to work with command line user input? Another cheatsheet:

// Command Line User Input in Golang
// REFERENCE: https://stackoverflow.com/questions/20895552/how-to-read-from-standard-input-in-the-console
package main
import (
"fmt"
"bufio"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print(">>> ")
userInput, _ := reader.ReadString('\n')
fmt.Println(userInput)
fmt.Print(">>> ")
userInput2, _ := reader.ReadString('\n')
fmt.Println(userInput2)
fmt.Print(">>> ")
userInput3, _ := reader.ReadString('\n')
fmt.Println(userInput3)
}
view raw userinput.go hosted with ❤ by GitHub

Need to ping a JSON API? More cheatsheet goodness from the folks at RapidAPI:

package main
import (
"fmt"
"net/http"
"io/ioutil"
)
func main() {
url := "https://community-hacker-news-v1.p.rapidapi.com/item/8863.json?print=pretty"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("x-rapidapi-host", "community-hacker-news-v1.p.rapidapi.com")
req.Header.Add("x-rapidapi-key", "472e13f6d7msh84492a2444096d4p1edbffjsn3ff4c1075778")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
}
 

Auth0: Do users need to signup? Or can they just log in?

This dev tip is brought to you by the developer of Moolabeast: Automated Expense Tracking for Android and iOS. Avoid overspending when you start tracking your expenses with Moolabeast.

Tonight using Auth0 I started integrating social login with Google, Facebook, and Twitter into the latest development version of my personal finance app, Moolabeast, and found myself wondering: Do users need to signup? Or can they just log in?

While testing this new integration it appears there is no difference between signing up and logging in using a social media account. However, I really wanted to make sure that I get this right so I decided to dig a little deeper into the issue. Long story short: there is no difference between signing up and logging in. If a user clicks on “log in” instead of “sign up”, the end result is the same. Apparently Auth0 has just stuck with this old signup/login paradigm as it’s what internet users are used to. First I sign up, then I log in.

So I hope this clears up some confusion if you happen to be integrating social login with Auth0 into your app. Happy hacking my friend!

 

The 100 Year Historic Decline of Dividend Payout Ratios Amongst S&P 500 Companies

Welcome back internet friends!

This is actually a cross-post of a comment I posted recently to Hacker News. Unsurprisingly, the comment received negative 1 upvotes. However, I think this is an import piece of information that has gone missing from the World Wide Web, so I wanted to bring it back: The 100 Year Historic Decline of Dividend Payout Ratios for S&P 500 Companies

The hacker news thread on which I commented was regarding a recent headline on increasing S&P 500 Buybacks, but what I think is more troubling is the decline in dividend payout ratios, not the increase in buybacks. But the two are closely related, so let’s take a look:

100 years ago, companies on average dispersed 90% of their earnings back to shareholders as dividends. But by the 1970’s however that number fell to below 50%. Fast forward to the 2000’s and the payout ratio is down to around ~30%.

What happened? Companies used to distribute their profits back to their owners, the shareholders. But that isn’t what’s happening anymore. In my amateur historical analysis, I believe there are two causes for this disastrous trend:

1) Tax dodging
2) The Casino/Gambling Mentality of Stock Market Investing

My theory is that the trend started with investors 50 to 100 years ago looking to dodge taxes, and gain a little free compounding. Makes sense! Right?

But it’s just gone too far. Now investors receive almost nothing for owning a stock. Take Apple Inc. for example. In 2019 Apple made a profit of $55 billion. How much of that did they pay back to their shareholders? $14 billion.

And Apple isn’t even the most egregious case. They at least pay a dividend. Google, Amazon, and Facebook combined paid $0 back to their shareholders this year!


So how has this insanity continued to go on for so long? The answer is simple: Adults don’t know what stocks are. They think that a stock is a thing that you buy, its price goes up and down, and the game is to try and sell the stock when the price goes up, and hope that the price doesn’t go down. And the machine that keeps the whole thing moving along is the Stock Buyback.

REFERENCE: The Dividend Story