bekkidavis.com

Discover the Essentials of Go Programming in 2022

Written on

Chapter 1: Introduction to Go Programming

In 2022, embarking on a journey to learn a new programming language can be exciting and rewarding. Go, an open-source language, simplifies the development of reliable and efficient software. This article aims to provide you with a foundational understanding of the Go Programming Language.

Go Programming Language Basics

Getting Started with Go

To begin using Go, the first step is to install it. You can find the installation guide at this link.

Writing Your First Go Code

Go organizes programs into packages, which are collections of source files located in the same directory. Any functions, types, variables, and constants defined in one source file can be accessed by other files within the same package.

For a simple program, follow these steps to set up your module path and create a ‘go.mod’ file:

$ mkdir go-project

$ cd go-project

$ go mod init go-project

This creates a new module called go-project. You can verify it by checking the contents of go.mod:

$ cat go.mod

module go-project

Next, create an app.go file in your directory, containing the following code:

package main

import "fmt"

func main() {

fmt.Println("Hello Medium")

}

You can now install this program using the Go tool:

$ go install go-project

This command compiles the go-project command and produces an executable binary.

Source Control

If you plan to use a version control system, this is an opportune time to initialize a repository, add your files, and make your first commit. However, using source control is not mandatory for writing Go code.

$ git init

Initialized empty Git repository in go-project/.git/

$ git add go.mod app.go

$ git commit -m "initial commit"

Importing Packages

To utilize external packages, you can specify how to fetch the package source code using a version control system such as Git:

package main

import (

"fmt"

"github.com/google/go-cmp/cmp"

)

func main() {

fmt.Println(cmp.Diff("Hello World", "Hello Go"))

}

When executing commands like go install, go build, or go run, the Go tool will automatically download the necessary remote module and log its version in your go.mod file.

Variable Declaration

In Go, within a function, you can use the := short assignment syntax instead of a var declaration with an implicit type:

package main

import "fmt"

func main() {

var i, j int = 1, 2

k := 3

c, python, java := true, false, "no!"

fmt.Println(i, j, k, c, python, java)

}

Go supports several fundamental data types, including:

  • String: A sequence of characters
  • Integer: A non-decimal number
  • Float: A decimal number
  • Boolean: TRUE or FALSE values
  • Array: A collection of values of the same type

Basic Operators

Go includes several basic operators:

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulus)
  • ** (Exponentiation)

Working with Arrays

Go features a data structure known as arrays, which can hold a fixed-size sequential collection of elements of the same type.

To declare an array, use the following syntax:

var variable_name [SIZE] variable_type

For example:

var balance = [2]float32{100.22, 500.03}

Loops

Loop statements allow you to execute a block of code multiple times. Below is a simple infinite loop example:

package main

import "fmt"

func main() {

for true {

fmt.Printf("This loop will run forever.n")

}

}

Recursion

A recursive function is one that calls itself. Here’s a basic example:

func recursion() {

recursion() // function calls itself

}

func main() {

recursion()

}

You can also create functions like factorial using recursion:

package main

import "fmt"

func factorial(i int) int {

if i <= 1 {

return 1

}

return i * factorial(i - 1)

}

func main() {

var i int = 15

fmt.Printf("Factorial of %d is %d", i, factorial(i))

}

Wrapping Up

After reading this article, I hope you now feel equipped to write simple Go code and have a clearer understanding of what Go programming entails.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Unraveling the Enigma: The Strange Case of Annie Börjesson

Explore the mysterious death of Annie Börjesson, a Swedish expat, and the theories surrounding her tragic end.

Understanding the Duality of Aliens: Good vs. Evil Forces

Explore the contrasting types of aliens and their impacts on humanity, examining the concepts of free will and alien treaties.

Discovering Ikigai: A Path to Purpose and Joy in Life

Explore the concept of ikigai, a Japanese philosophy of finding meaning in life, and how it can lead to happiness and fulfillment.

Revolutionizing Project Management: Unleashing Airtable's Power

Discover how Airtable transforms project management and enhances collaboration for businesses of all sizes.

The Hidden Costs of Overconfidence: Insights from Marathon Runners

Explore how overconfidence affects performance, as revealed by a study of marathon runners and their self-assessments.

Unlocking Self-Confidence: Understanding and Overcoming Insecurity

Explore the key reasons behind low self-confidence and learn practical strategies to overcome insecurity.

Exploring the Internet of Technology: Embracing Diversity in Innovation

Discover how the Internet of Technology fosters an inclusive community for sharing diverse technological insights and ideas.

Intriguing Developments in the Skies: Ukraine and Israel's Challenges

Explore the recent incidents involving Ukraine's downing of a Russian plane and the challenges faced by Israel's military.