Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Go Language for Beginners in 16 Parts!

DZone's Guide to

Go Language for Beginners in 16 Parts!

In this post, we'll learn how to get up and running with Go, and do some neat things like call functions and use third-party packages. Let's go!

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Hello!

This is the first post from the Golang Series: Go Language for Beginners in 16 Parts!

Go is one of the most important, exciting, and easy programming language to learn!

Its syntax is concise, really clean, and follows many patterns that keep us away from fighting pointless things, like indentation, error handling, concurrency, and so on.

Before jumping crazy into a lot of hacked code, let's understand a few concepts about Go Lang!

Go Characteristics


Go was created by Google in 2009. Why? How was it resolved then?

Go was designed to be really simple to read.

It brings the readability of Dynamic Programming Languages.

Go was designed to be really simple to write.

It doesn't have magical and boilerplate code and has limited structural typing.

Go was designed to be really fast.

Go is written in C under the hood!

Go is compiled

You don't need to have an Interpreter or a Virtual Machine and you don't need to make any changes to have your Go code run in such environments.

Go is a Statically Typed Programming Language.

Even as a statically typed language, Go has a clean syntax and a great type inference, close to a Dynamic Language.

Go has a Garbage Collection.

Great! You don't need to remember when to use malloc to allocate memory!

Polymorphism in Go is great!

Go follows a different approach to deal with Polymorphism, the concept of Interface. This is really simple, it's just a collection of methods!

Go supports Networking and Multiprocessing in a beautiful way.

Go was designed to have a built-in Concurrency approach with really lightweight processes with Goroutines and Channels

Hello, Go!

This will be our first Go code! We should create a file called go-hello.go

package main

import "fmt"

func main() {
    fmt.Println("Hello, Go World!")
}

Let's compile the code

$ go build go-hello.go

After that, a file will be created in the current directory named go-hello. This file is the compiled code that can be executed, as you can see below:

./go-hello

This 2 step approach is a little bit boring and Go can do it in just 1 step, using the go run command:

$ go run go-hello.go
Hello, Go World!

So, you don't want to try Go on your own machine? Don't worry, we can play around with it in another place!

Running Go Code in the Go Playground

I can't talk about Golang without mentioning its great Playground.

Go there and run this same code by just clicking on the Run link!

Awesome, isn't it?

Packages and Functions in Go

Did you notice that we've called the function Println? This function comes from the package fmt.

This package is a built-in package, provided in Golang by default. To use it, we just need to use the import keyword with the name of the package

import "fmt"

Let's call a function from another package, in this example, from the package math:

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Println(math.Max(9, 5))
}

Run this code in the Go Playground or on your local terminal:

go run hello-math.go

The output should be: 9

Calling Functions in Go

We can call another function in the same file:

package main

import "fmt"

func main() {
    fmt.Println("Calling the function: main")

    anotherFunction()
}

func anotherFunction() {
    fmt.Println("Calling the function: anotherFunction()")
}

If you run this code you'll get the following result:

Calling the function: main
Calling the function: anotherFunction()

Using Third-Party Packages in Golang

Go has a simple way to get an online package from GitHub. Just follow the pattern:

go get github.com/go-awesome-library-address

What about printing messages with Emojis in Go? Well, this is not a required and important feature (though I think it is) in the Golang core, so let's use a package from GitHub.

We'll follow the pattern from above:

go get gopkg.in/kyokomi/emoji.v1

Now it's time to print Emojis in Go:

package main

import (
    "fmt"
    "github.com/kyokomi/emoji"
)

func main() {
    fmt.Println("Hello World Emoji! :tada:")
    emoji.Println("Do you like :coffee: ? Of course you like!")
}

The output will print out emojis!

Go Is a Statically Typed Language

This means that Go knows which type your values are! String and Integer are both different from the Go perspective!

What about printing a String with an Integer? Let's create a file called types-in-go.go with the following code:

package main

import "fmt"

func main() {
    fmt.Println("Hey " + 300)
}

Now, run the code:

$ go run types-in-go.go

You will get the following error message:

./types.go:6: cannot convert "Hey " to type int
./types.go:6: invalid operation: "Hey " + 300 (mismatched types string and int)

This error occurs at compile time. Try to just compile the code:

$ go build types-in-go.go

The same error will occur! Don't worry, we'll fix this code later. 

Showing the Type of a Value With TypeOf

What about printing out the type of a value? Let's do that by using the TypeOf() method from the built-in reflect package:

package main

import (
    "fmt"
    "reflect"
)

func main() {
    fmt.Println(reflect.TypeOf(1))
    fmt.Println(reflect.TypeOf(9.5))
    fmt.Println(reflect.TypeOf("Just a String"))
    fmt.Println(reflect.TypeOf(true))
}

If you run the code the result will be:

int
float64
string
bool

Great! In this simple post, we got a little bit inside to the Go Language World! We have much work to do in 16 Parts!

In the next post, Part 2 - Declaring Variables in Go, we're going to see how to use Type Declaration in Go!

That's it! I hope that was useful to you! Thank you!

Follow us to keep up to date! 

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
go ,golang ,golang development ,web dev

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}