Lightning Quick Introduction to Go

In this lesson, we'll briefly introduce Go and its syntax. This is just to whet your appetite. We will dive much deeper into Go later.

Lightning quick introduction to Go

Go has C-like syntax. This means that on the surface Go code looks very much like C, C++, C#, or Java code. However, there are no semicolons and much fewer braces. Go also supports auto inference, which cuts significantly on boilerplate typing.

Go is a multi-paradigm language that supports imperative (procedural), object-oriented and functional programming. It has a solid standard library, a very active community, and good integration with C.

Here is a little code snippet that shows many of these capabilities. This is just a taste, and we’ll write and dissect code in more detail very soon…

// Here is a single line comment
/* 
 And here is 
 a multi-line 
 comment 
*/

// Every source file starts with a package statement (after optional
// comments and empty lines).
// The 'main' package designates as executable as opposed to a library.
package main

// The 'import' statement declares the libraries used by the source file
import (
  "fmt"            
  "errors"   
)

A function takes zero or more arguments and can return zero or more values as a result.

func triple(x int) int {
  return x * 3
}

Functions often return the error interfaces to designate success or failure, nil, and non-nil, respectively.

func check(fail bool) error {
  if fail {
    return errors.New("I'm a failure") // failure
  }
  return nil // success
}

The private variable can be used only in the package it is declared in because its name starts with a lowercase character.

var private string = "I can be used only in this package"

The Public variable can be used from any package because it is capitalized.

var Public string = "I can be used from anywhere"

The PublicFoo function can be called from anywhere because it is capitalized just like variables.

func PublicFoo() {
  fmt.Println("foo here")
}

The main()function is the entry point to the program.

func main() {
  x := 5 // Go can infer that x is an int when using := assignment
  result := triple(x) 
  fmt.Printf("%d * 3 = %d\n", x, result)

  PublicFoo()
}

Ofcourse, there is a lot more to Go, but this should get you started.

Conclusion

In conclusion, Go is a simple yet exciting programming language that has a lot of momentum behind it because it just works. It is especially great for command-line programs because it generates high-performance, native executables and doesn’t require any runtime or external dependencies. It’s also a very productive language, which is important for command-line programs that often don’t get the same amount of attention as bigger systems.

Quiz

1

How do you make a Go function public (can be called from any package)?

A)

Make sure its name starts with a lowercase letter

B)

Make sure its name starts with an uppercase letter

C)

Add the keyword “export” before the function name

Question 1 of 30 attempted