Go for Beginners — Part 2

An Introduction to Go

Photo by Fotis Fotopoulos on Unsplash

Compiling

When you write Go code, we’re writing it to be readable for developers. However it’s not readable by computers.

go build nameOfFile.go

Running Our Code

Go also gives us the ability to automatically run our code after compiling with the command.

go run nameOfFile.go

Packages

So now we are starting to take a deeper look into a Go programs structure.

Importing Packages

Cool so now we need to import some basic packages for use to be able to do anything within Go. So lets import the fmt package for us to use.

// main.go
package main
import "fmt"

Functions

Functions are things we can call upon to run a specific piece of code when we want too.

func nameOfFunction() {
// Write Code Here
}

Hello World

Lets put this all together now,

// main.gopackage mainimport "fmt"func main() {
fmt.Println("Hello World!")
}
$ go run main.go
Hello World!
>

Importing Multiple Packages

If we want to import multiple Packages in go we can use a tiny bit of a different syntax to achieve this.

import (
"package1"
"package2"
)
import (
package1Allias "package1"
package2Allias "package2"
)
package1Allias.someFunction()
p1.someFunction()

Comments

Comments in Go are similar to many other languages by using the keyword // this comments off the entire line after that keyword and it will not be read by the compiler

// Hello!
package main
// import "fmt" (This line won't be read)

Summary

That is a basic introduction to the foundation of Go and it’s little quirks compared to other languages.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kodey Thomas

Kodey Thomas

Backend Developer, Probably listening to music and drinking coffee