Skip to content

Guilospanck/go-language-essentials

Repository files navigation

Go Language Essentials

Here are the basic essentials things needed for getting started with Go Language.

How to install Go

  • If you are using WSL2, follow this Medium post (if you also are using zsh, paste the lines into .zshrc);
  • If you are using MAC, Windows or Linux, go to the GoLang website.

Getting started

Initializing a module:

Every new application need a module manager (something like a package.json). So run the following command to create the go.mod file:

go mod init {module_name}

Usually {module_name} will be where your application is located, something like github.com/Guilospanck/GoLanguageEssentials. This command is similar to yarn init


Import modules from the internet:

You can go to the Go dev website and search for the package you need (something similar to yarn, npm, pypi). Search for the module you want and then import it in you .go file.

import "module/name"

After doing this, you need to run a command to actually install that module (similar to npm install or yarn install):

go mod tidy

To run your .go file:

go run file.go

Testing in Go:

  • You have to create a new file with the name of the file you are testing, appending _test at the end. Example:
greetings.go  => greetings_test.go

To run your tests, go to the directory wanted and run:

go test
or
go test -v  # to verbose

Compiling and installing the application

  • To generate a binary executable, you can run (in your file directory):
go build

This will generate a binary file with the name of you Module. You can run this binary file as it follows:

./GoLanguageEssentials    (./{binary_name})

But now, to be able to run it from anywhere in your computer, you must install it:

go install

Now, you can run it from anywhere just typing:

GoLanguageEssentials    ({binary_name})

Some observations about Golang:

  • The first line when you're writing a code in Go is package {packageName}. It must exists only a package per directory. If you wanna have a different package name for a file, you must change its directory. Every Golang program must have a file in which there is the main package with the main function.

  • Functions that begin with an upper-case letter are exported by default, hence it can be used in other packages that import our package.

  • The := (short assignment statement) can be used in place of a var declaration with implicit type. Outside a function, every statement begins with a keyword (var, func, and so on), and so the := construct is not available.

  • Each source file can define its own init function to set up whatever state is required (actually, each file can have multiple init functions). init is called after all the variable declarations in the package have evaluated their initializers, and those are evaluated only after all the imported packages have been initialized.

Go's basic types:

  • bool
  • string
  • int (all positive and negative values) => (int8 16 32 64)
  • uint (only positive values) => (uint8 16 32 64 ptr)
  • byte (alias for uint8)
  • rune (alias for int32. It represents a Unicode code point)
  • float32 float64
  • complex64 complex128

For 32-bit systems, int uint and uintptr are 32 bits wide. For 64-bit systems they are 64 bits wide (usually).

Zero values:

Variables declared without an explicit initial value are given their zero value. The zero value is:

  • 0 for numeric types;
  • false for the boolean type, and
  • "" (the empty string) from strings.

About

Here are the basic essentials things needed for getting started with Go Language.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages