maingo

Send a Tweet with Go! Part 1

Here’s to a ‘hello world’ of another sort for RadberryPi.Tech, our first code tutorial. Since this is my first let’s keep it easy.

Go, or golang for searchability’s sake, is one of my favorite languages these days (jan,2018), mostly because it is fast fast fast! Portability and cross compiling (say, building an app for Windows from a Linux box or vice versa), built in concurrency handling, did I mention the speed? And I don’t just mean the apps are fast. Writing them goes PDQ, too, or at least it can. We all know it doesn’t take much to tank a software project, but that is a series of posts all it’s own!

What kind of app to build? Golang apps run from the console, at least they can (and normally do). No need to rock the boat there, yet. How about one that tweets deals on hobby electronics? Raspberry Pi’s perhaps? Now we’re getting somewhere! So far we have:

Obviously you’ll need to install Go. You will also want an editor that is suited for development.  Which one is the best? Careers have been made arguing that point, but for our purposes VSCode works.  As does any regular old text editor. Once you have the language installed, you can work right out of the console if that’s what you want to do, cowboy. Nobody cares. 

After completing the installation, you will have a fully functional Go coding environment as explained in the docs*.  If you haven’t noticed the pattern yet, documentation is huge in the programming world. 

Now we need a local copy of Anaconda, so we can reference it in our code and save ourselves literal weeks of coding time. Weeks. That’s real. Literal. To do that, run the following command from your command line:

go get -u github.com/ChimeraCoder/anaconda

Now fire up your editor of choice and create a file named main.go. You can put it pretty much anywhere, but if you READ THE DOCS, you’ve got them in your $GOHOME directory somewhere. Similarly, finding the command to send a tweet took a little digging around in the Anaconda docs, but it’s all there if you are willing to click a couple times.

Combining all of this together, we now have a file named main.go in our project folder. We would like to tweet a Hello World message so we can kick this thing into high gear and make billions of that sweet innernet goldbits or whatever. We need to reference the Anaconda package, authorize our Twitter App (you did remember to set that up, right?), and send our message to the world. Your code will look something like this:

package main

import (
	"github.com/ChimeraCoder/anaconda"
)

func main() {

	anaconda.SetConsumerKey("your-consumer-key")
	anaconda.SetConsumerSecret("your-consumer-secret")
	api := anaconda.NewTwitterApi("your-access-token",
		"your-access-token-secret")

	api.PostTweet("Hello World from Golang! ", nil)
}
Hello World from RadberryPi.tech using Golang!
Hello World from Golang!

 

*RTFM

Read the docs. They have been meticulously prepared and organized, and have more info than our puny little human brains can process simultaneously. They have all of the answers.  If you don’t read the docs, you will be alone forever.  Your pets won’t even like you any more. And if you go to Stack Overflow to ask something that is right there in the docs, well bless your heart.

Get Used to Rejection

You won’t write perfect code the first time. In fact, you may write bad code for a long time. Your code is going to break. Feel it.  Let the failure wash over you. Learn why it didn’t work. Make an earnest effort to understand the cause, avoid voodoo code (cargo cult maybe), fix the cause, and move on.

This is a first draft and will be edited over time.  Hope you like it!

%d bloggers like this: