DEV Community

b.chau504
b.chau504

Posted on

Going Down the Gopher Hole. Intro to Go

As developers, when we are debugging, we always end up turning to Google for answers. To no surprise, it's the world's most used search engine. Google is also part of FAANG(Facebook, Amazon, Apple, Netflix, Google), some of the world's largest, most well-known tech companies. Many individuals that start their journey as a software developer aspire to work for one of these big tech companies, like Google.

So what does it take to work for a company like Google? Some of the main programming languages used within Google include C++, Python, Java, and Javascript. You may have heard of some of these well-known, general languages, but there is a language that Google uses in several of their internal projects that include Chrome, App Engine, Earth, and Youtube. This language is called Go(or GoLang for better search search results). It was developed at Google, so learning Go will definitely give you an advantage in the interview process.

Go is an open-sourced, compiled, and statically typed programming language. It was designed to improve the programming productivity within Google and allow developers to create dependable or concurrent applications. Because it compiles quickly, many programmers feel that they can get a lot more done.

Go's simplicity and readability are among the reasons it quickly gained popularity and became many developer's first choice. It is simple to learn, especially if you have prior experience with another programming language.


Getting Started with Go

Install

To get started using Go, we would need to download and install it.

Once installed, verify that Go has been installed by opening your terminal and run the following command:

$ go version
Enter fullscreen mode Exit fullscreen mode

Note: For the changes to take effect, you may need to restart your terminal.

If the command prints the installed version of Go, you can move on to the next step.

Setup

cd into the directory that you'd like to create your go project.

cd %PATH%
Enter fullscreen mode Exit fullscreen mode

Create a directory for you Go source code

mkdir gobasics
cd gobasics
Enter fullscreen mode Exit fullscreen mode

When your code imports packages from other modules, you manage those dependencies in your own module. A go.mod file defines that module, which tracks the modules that offer those packages. That go.mod file is kept with your code and in your source code repository.

Run the go mod init command and specify the name of the module your code will be in to enable dependency tracking.

In development, the module path is usually the repository location where your source code is stored. The module path, for instance, could be github.com/mymodule. If you intend to make it available to others, the module path must point to a location where Go tools can download it.

We will be using example/gobasics.

$ go mod init example/gobasics
go: creating new go.mod: module example/hello
Enter fullscreen mode Exit fullscreen mode

To open your text editor, in the gobasics directory, run the command:

$ code .
Enter fullscreen mode Exit fullscreen mode

In your text editor terminal create a file to begin writing your code.

$ touch gobasics.go
Enter fullscreen mode Exit fullscreen mode
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
Enter fullscreen mode Exit fullscreen mode

The above code is a basic setup.

  • We start by declaring main package to create a standalone executable. (A package is a method of grouping functions that is made up of all the files in the same directory)

  • Afterward, we would import the fmt package, which has tools for formatting text, like printing to the console. When you installed Go, you received this package as one of the standard library packages.

  • Finally, create a main function that will print a message to the console. When you launch the main package, a main function is executed by default.

Let's give it a try. Run the following command in your terminal:

$ go run .
Enter fullscreen mode Exit fullscreen mode

Basic Syntax

Let's get into some of the basic syntax of Go and see how easy it is to learn.

Printing

package main

import "fmt"

func main() {
  var i, j string = "Super", "man"

  fmt.Print(i)
  fmt.Print(j)

}
Enter fullscreen mode Exit fullscreen mode

The Print() function prints its arguments with their default format, so this will output Superman in one line.

package main

import "fmt"

func main() {
  var i, j string = "Super", "man"

  fmt.Print(i, "\n") // => Super
  fmt.Print(j, "\n") // => man
}
Enter fullscreen mode Exit fullscreen mode

Use \n if you want to print the arguments on separate lines.

fmt.Print(i, "\n", j)
Enter fullscreen mode Exit fullscreen mode

It is also possible to print many variables with a single call to Print().

fmt.Print(i, " ", j)
Enter fullscreen mode Exit fullscreen mode

Use " " to separate string arguments.

package main

import "fmt"

func main() {
  var i,j = 10,20

  fmt.Print(i,j)
}
Enter fullscreen mode Exit fullscreen mode

If neither argument is a string, Print() places a space between the parameters.

package main

import "fmt"

func main() {
  var i,j string = "Super","man"

  fmt.Println(i,j) // => Super man
}
Enter fullscreen mode Exit fullscreen mode

Then there is Println(), which is a derivative of Print() that adds whitespace between the arguments and a new line at the end of the output.


Variables

package main

import "fmt"

func main() {
  var favFood string = "sushi"
  x := 4

  fmt.Print(favFood) // => sushi
  fmt.Print(x) // => 4
}
Enter fullscreen mode Exit fullscreen mode

Variables are declared using the var keyword, followed by the variable name and type. You must always specify either type or value or both.

Using the := sign, the type is inferred from the value, meaning the type is determined by the value by the compiler.
When using the := sign, you must always assign a value to it.

Unlike the var keyword, := can only be used inside functions and variable declaration and value assignment has to be done in the same line.

package main
import "fmt"

func main() {
  var a string
  var b int
  var c bool

  fmt.Println(a) // => 
  fmt.Println(b) // => 0
  fmt.Println(c) // => false
}
Enter fullscreen mode Exit fullscreen mode

A variable's value will be set to the type's default value if it is declared without an initial value.

package main

import "fmt"

func main() {
  var a, b int = 6, 2

  fmt.Println(a) // => 6
  fmt.Println(b) // => 2

  c, d := 4, "Ever"

  fmt.Println(c) // => 4
  fmt.Println(d) // => Ever
}
Enter fullscreen mode Exit fullscreen mode

Several variables can be declared in a single line. When using the type keyword, you can only declare one type of variable per line. Otherwise, you can declare different types of variables in the same line.

package main
import ("fmt")

func main() {
   var (
     a int
     b int = 0
     c string = "seven"
   )

  fmt.Println(a) // => 0
  fmt.Println(b) // => 0
  fmt.Println(c) // => seven
}
Enter fullscreen mode Exit fullscreen mode

For better readability, several variable declarations can be gathered within a block.


Arrays

package main

import "fmt"

func main() {
  var arr1 = [4]int{1,2,3,4}
  arr2 := [5]int{5,6,7,8,9}
  var arr3 = [...]string{"Pomeranian","Husky","Shiba","German Shepherd"}

  fmt.Println(arr1[2]) // => 3
  fmt.Println(arr2[0]) // => 5
  fmt.Println(arr3[1]) // => Husky
}
Enter fullscreen mode Exit fullscreen mode

When declaring an array you must specify the length of the array by number like arr1 and arr2 or its inferred like arr3. A specific array element can be accessed by using the index number.

Top comments (0)