Go-Lang

So I’ve started to poke around Google App Engine and Go.  Honestly, I like it.

1. Thoughts

2. Why

3. Little Tutorial

Go

Go, or commonly search, “golang” is a language made by Google.  Dah dah dah, I’m not a guy who knows much about the language, but I’ve read about it, and don’t want to give false information about it, so read it’s Wikipedia page if you’d like.  I decided I wanted to learn Go several months ago.  Problem was, no reason to learn it.  Not that I don’t enjoy learning new things on my own, I just can’t think of project ideas to actually put it to use.  But I found out about Go several months ago, followed it a bit for about a year, then decided I should learn it.

Go, the language, I do enjoy writing in.  I did some of the go-tour (It got boring after number 17 a couple months ago and couldn’t bring myself past fixing Pi in the tour this time around), but decided that I’d just jump in and go for it.  I usually learn this way, unless it’s something that’s massive and complex, in which case I try to find break down tutorials.  But this was actually successful, I’m getting the hang of the syntax and differences from other languages surprisingly quickly.  Again, I’m not doing anything dramatic with Go, but compared to how I feel learning other things, this went very smooth.

Why Go/GAE?

So I’m working on my Android app and I need some multiplayer work done (Nothing major, honestly just submit some info, get some info, pretty basic).  I looked around for some platforms I can get this on (Mostly to test and get it working locally).  I though of Google App Engine and Amazon’s EC2.  Amazon, your interface sucks.  I’m sorry, but honestly, it’s a massive wall of text with what seems (To me, a guy who just wants to make a small app) like a lot of worthless data that I won’t want or need. Google App Engine however, looks appealing, secondly, it’s more structured and organized, and I feel I can actually do something with it.  That’s honestly why I chose what I chose, nothing to do with specs (I’m making a small app that I don’t expect more than a thousand people playing, I don’t need any fancy stuff, just something that works).  Further, GAE supports Go.  I’m slightly certain you could get it to work, maybe with some hassle, but for what I’m planning, I’m sure it’s more than able.  If not, I could just use good ol’ PHP.

Tutorial, how to use POST/GET, and what about the datastore?

Below, I’m omitting package, imports, and the init, simply because the basic tutorial on GAE goes over this pretty well.

type Griddler struct {
	Id         string
	Author     string
	Name       string
	Rank       string
	Difficulty string
	Width      string
	Height     string
	Solution   string
}

func createHandler(w http.ResponseWriter, r *http.Request) {
	//Creating a Griddler to add to datastore.
	c := appengine.NewContext(r)
	parsed, err := url.Parse(r.URL.String())

	if err != nil {
		fmt.Fprintln(w, "Bad stuff happened in the create handler\t")
	}
	q := parsed.Query()       //Parse the URL.
	griddler := new(Griddler) //Create and set all the data in your griddler.
	griddler.Id = q.Get("id")
	griddler.Author = q.Get("author")
	griddler.Name = q.Get("name")
	griddler.Rank = q.Get("rank")
	griddler.Difficulty = q.Get("diff")
	griddler.Width = q.Get("width")
	griddler.Height = q.Get("height")
	griddler.Solution = q.Get("solution")
	k := datastore.NewKey(c, "Griddler", griddler.Id, 0, nil) //Make a key based on the Id of the Griddler.
	if _, err := datastore.Put(c, k, griddler); err != nil {
		fmt.Fprint(w, "Error during adding item in data store.  Sorry mate.\t", err)
	} else {
		fmt.Fprintf(w, "Added: %s with key %s", griddler, k) //If no error putting in, send back a success message.
	}

}

So, Honestly this code isn’t too interesting. You parse the url from the request, then you just use the query to set some data. Next you get a key for this Griddler (Please note, all Id’s will be a different hash, so first I’ll want to check if this hash exists).

Beyond this, you Put the griddler in the datastore, and you output the result. Prior, I wrote my own URL parser, which I mean, great and all because I control what the app does, but I’d much rather not doing this.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s