GOGOGOGO….
GO!
I was told by a few people who all seem to know what they’re talking about, that once you’ve hammered in your first couple programming languages, the rest just “come naturally”. Well, the only things that have ever occurred naturally to me are disasters and my unlucky proximity to them. The amount of times I’ve narrowly avoided literal lightning strikes is kind of absurd. Nevertheless, I’m putting myself through the ultimate test of the “naturalizing” theory. Who doesn’t need a little self-sabotage every now and then? You see, as a spastic learner, my attention tends to wane in differing patterns throughout the learning process. By the time I feel fluent in one topic, the knitted path it took to achieve any mild successes has weaved wildly into a patchwork of utter functional chaos. Oh, and it takes a very long time. It’s for that reason that I’ve chosen to throw gas onto the fire currently burning away at the threads of my remaining patience (damn you, Rails) and jump…in the fire. Some how…some way… this is probably scholastic therapy.
I want to exclaim that the purpose of this is not to master Go as quickly as possible, but rather to draw some parallels to other concepts in coding that I am already familiar with. Though, one day, when I’m less swamped with other obligations, I’d love to set time aside to give it an honest attempt. I’ve given myself a 3-hour time limit to gather as much info as I can about key points in Go and spit them out into this blurb. Huge shoutout to freecodecamp and golang.org for providing me with really useful resources to suck all this in through.
Why 3 hours? That’s about as much time as I’m willing to spend on this assignment to account for other pressing responsibilities handed to me by bootcamp. You’ll just have to trust me on the timing thing. Why Go? A quick bit of research tells me it’s a statically-typed language masquerading as a dynamically-typed language with tons of cool features and a similar likeness to C and C++ syntactically, and I know a tiny little bit about those two. That, paired with me being a pretty big fan of Google, who are responsible for creation of the language, pretty much sealed the deal. I’m going to jot down every little aspect that rings a bell or correlates to some other data type from a language I’m better equipped to translate.
We’re Learning* GO in 3 hours, people!
This is going to be unfiltered bullet-point notes that I take down after each term is conveyed. I WILL NOT edit these aside for typos and sentence structure so they remain legible. I will also take some screenshots of my code and add them to a repo at the end. Fun for the whole family. Enjoy.
History — A language with a huge community of supporting programmers used to code out everything from databases to intricate web and mobile apps. Developed by a small group of Google devs in the early 2000’s as a way to reduce dependency on languages such as Java and C++ due to slowing speeds caused by years of legacy code workarounds and language updates. It’s built to be fast, super powerful, efficient and easy to read.
Compilation — When setting up a Go environment, it is necessary that the root file of source code is named “src”. The compiler searches specifically for this path at runtime. It will also look for a folder called “bin” to store binaries created by compilation and “pkg” to store temporary dependencies. Highly recommend this link for setting up a quick local environment to run Go. If you just want to play around with it for a while the official tour is the way to go. The file suffix is always “.go”. Setting this whole environment up locally took up the majority of my 3-hour time limit.
Strong and Statically Typed — Strong typing means the type of a variable can not change over time. A string stays a string forever. A boolean stays a boolean forever, etc… UNLESS it is explicitly converted to be something else. Statically-typed means all variables have to be defined at compile time and may not change as state-based actions.
package main — Every Go application is structured into package dependencies and need to be declared and included at the start of the file.
import “fmt”— Import is used at the beginning of a file to import additional libraries based on the application you’re building out. There is literally a dependency folder named “fmt” (short for “format”) in my environment setup that has to tell Go how to render string, integer and boolean literals. As shown above fmt.Println(“Hello World”) prints a basic string to the console at first.
func — The canon way to begin declaring a function in Go. I know, it’s pretty self-explanatory, but I felt the need to specify that this is not the way to spice up your code. Or is it? This appears to be very similar to the way most languages begin a function. A function named “main” is conventionally appropriate for the main document of any project.
var i int — This starts looking a little funny. Variables begin declaration by first telling Go that you are starting a variable declaration. Secondly, the name of the variable is stated “i”. This seemed a little backwards to me at first, but the rationale here was to be able to write it out the same way you would say it. “Variable i is and integer”. This is what was meant by statically-typed. Javascript was able to skip over this step by being a dynamically-typed language with its super smart variables. Not only do you need to specify the type, you also need to assign a value to the variable. We’ll go with 42 and send it to the console. BUT WAIT…
i := 42 — Getting a little worried that it took 3 lines of code to console log a variable. Thankfully that’s not the case or even best practice. This can all be done on the same line by using a := to declare a variable. this tells the variable type to be automatically set to whatever data type falls on the right side of it and store that value to i. Now we’re talking.
Use Cases — The cool thing about static typing in go, is that you don’t need to initialize a variable with a value at the same time you declare it, much like a dynamically-typed language. You can declare it in one scope and initialize it in another.
Batch declaring variables — It’s no big surprise this can all be done in batches. One thing to note, not pictured in the example, is that all declared variables must be used or Go throws an error. Variables cannot go unused. React warns you about that sort of thing, but code will still run. Go forces you into being frugal with your variables. Good on them.
Shadowing — A term I was introduced to for the first time. This is the idea that if the same value is declared more than once, an error is not thrown. Rather the variable with the innermost scope takes precedence. If I were to reassign oneThing to something else inside the main function, the main function would be the assigned value at runtime. Scope being the only word I recognized here. No parallels to draw. Cool stuff.
Data Types — Data types in Go are a bit similar to other languages. With string, integer and booleans all serving the expected purposes. One thing you have more control over in this type of language however, is the amount of memory a certain variable occupies. For example: an int declaration in Go sets 32 bits of local memory aside for the value it stores. If you have or are expecting to have a much larger integer in this field, you may specify “int64” to make 64 bits available, or even “int8” to make only 8 bits available in memory. This rings a bell when thinking back to double and short variable types in C++ and JavaScript.
Arithmetic Operators — Plus, Minus, Multiply and Divide. All present and accounted for. Pretty much exactly how you’d expect.
Constants — Much like constants in JavaScript, the biggest difference between const and var, is that a const may not be reassigned anywhere. Further, in Go, const values must be declared at runtime. Not calculated at runtime, but declared outright before execution.
*Remember when I said I’d try learning a whole language in 3 hours? Well, 3 hours have passed and I almost grasped a tiny shard of how variables and operators work. C’mon now. I’m all about lowered expectations, people.
On a serious note, this functioned more like a mini journal than a single spew of carefully researched info and I thoroughly enjoyed making it. Making these connections, recalling some of these core concepts was a fun little sprint into unknown territory. Now I feel like the stake is planted for the next leg of the race.
Next time…
GitHub Scribble Repo: https://github.com/JZims/GoBlog