Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Baby Steps With Go Lang

10/7/2016

0 Comments

 
One day I was bored and derping around on codepen. I've heard people say that Go was a great server side language with the speed of C and the ease of use of JavaScript (also I'm just a huge google fanboy). I don't know if that's really true, but I definitely don't not like Go so far. The easiest way to get started in my opinion is to just load up coderpad.io, choose Go for your language, and just play around in the web IDE.

The Coderpad Starter

At the time of this writing, when you choose a new Go pad it starts you out with some simple code (scroll to the bottom if you want to see it all at once). I'll go through it step by step and then show it in all it's glory at the end. 

Comments

Right off the bat we can see that a double slash ( // ) is a familiar symbol that signifies the line should be treated as a comment.

// To execute Go code, please declare a func main() in a 

The Main Func

Reading the comment, we see that the function called "main" kicks off your program. The name "main" is important similar to Java's "main" constructor method that kicks off a class, but there are no classes like Java. Instead it's a func (aka function) similar to Es5 JavaScript or functional programming languages.  
func main() {
  
}

Package

​Also similar to Java (and also ActionScript 3), we have this name-spacing sort of deal that uses the package keyword. This is basically the directory path in which this file lives. 
package main
Putting "package main" means this file is in a folder called main (I think). Although for codepen it doesn't really matter all that much since you're usually just using one file in a codepen (at least I am, anyway).  

Imports

​Then we can import some libraries that we need. To be honest, I'm not really sure what fmt means, and I don't feel like looking it up [ok fine- it's just short for ​format]. Just know that in order to log text to the console you need to import this library. 
import "fmt"

Logging Text to the Console

We can then log text to the console by using the Println method.
fmt.Println("Derp!");

For Loops

Aside from that weird assignment symbol that uses a colon, for loops don't look all that weird- wait a minute; there are no parentheses in the for statement. That's a little weird...  Anyway, here's an example one of of these here for loopers: 
for i := 0; i < 5; i++ {
    fmt.Println("Hello, World!")
  }

All Together Now!

That's pretty much all you need to know to understand the snippet of starter code in your coderpad IDE when you switch to Go. You might as well buy a tee-shirt and call yourself a Go developer now. You're welcome. Lol.
// To execute Go code, please declare a func main() in a package "main"

package main

import "fmt"

func main() {
  for i := 0; i < 5; i++ {
    fmt.Println("Hello, World!")
  }
}

Bonus: Calling Functions!

Now that you know the ropes you might want to create some functions and call them. Well, just define new functions with the "func" keyword and call them by typing out their name followed by opening and closing parens. Notice that we can call a function before we define it in the code:
func main() {
  derp();
}

func derp() {
  fmt.Println("Derp!"); 
}

What Do You Think?

I think the Go syntax makes sense. At the moment it seems really similar to Java or JavaScript, but it feels like they tried really hard to make it as terse syntax as possible which I guess is good because you have less characters. It's really not a huge difference from writing the word function and looks a little funny not having parentheses in the for loop, but it doesn't bother me that much. hehe. Anyway, I need to look more into Go and actually use it to see what type of projects it would be good for, but at least for derping around in codepen it's a pretty nice, straight-to-the-point language. I like Go, and I look forward to using it more. How do you feel about Go Lang?
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Picture
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    March 2023
    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2023