How to start programming in GO

In this article we are going to talk about how to start programming in GO. First of all we will make a small introduction.

Introduction to Go Programming

Welcome to Go programming! Go is a modern, fast and easy-to-learn programming language. It is designed to be easy to read and write, and is an excellent choice for beginners. In this guide, we will cover the basics of Go, from variables to loops, conditionals, arrays, lists and dictionaries, as well as how to install modules and compile for other platforms.

How to create my first program in GO lang

Programming in Go is easy! If you want to learn how to program in this language, then you are in the right place! In this article we will show you how to do it and how to create a simple program.

To get started, you will need to download and install the latest version of Go from its official website. Once this is done, you will need to configure your environment to be able to use it. This includes adding the path to the Go bin folder to your PATH variable in Windows or adding an alias for the go command in your shell.

Once you’ve set up your environment, you’re ready to go! The first thing you’ll want to do is learn the basics of Go programming. This includes things like variables, loops, conditionals, and functions. These are the fundamentals of all modern programming languages and will help you better understand what you are trying to do when you program.

Now that you understand the basics, you can start creating your own program. Let’s start with something simple: a program that will print the message “Hello World” on the screen. To do this, you first need to open your favorite code editor and create a new file called “hello_world.go”. Then, write this code inside the file:

package main
import "fmt"
func main() {
fmt.Println("¡Hello world!")
}

This code is quite simple to understand: we are importing the fmt library and then calling its Println function to print the “Hello World” message on the screen. Once you have written this code, you can save the file and run it using the command go run hello_world.go in your terminal. If everything went well, you should see a message that says “Hello world!” on your screen.

Variables

Variables are an important part of any programming language. In Go, there are three main types of variables: integers, floats, and strings.

Integers are whole numbers, such as 1, 2, 3, etc. Floats are numbers with decimal places, such as 1.5, 2.3, etc. Strings are character strings, such as “hello” or “goodbye”.

To declare a variable in Go, we use the keyword “var”. For example, to declare an integer variable called “number”, we would write:

var number int

We can also assign a value to the variable at the same time we declare it. For example, to assign the value 5 to the variable “number”, we would write:

var number int = 5

Loops

Loops are an important part of programming. Loops allow us to execute a section of code repeatedly. In Go, there are two main types of loops: the for loop and the while loop.

The for loop is used to execute a section of code a specific number of times. For example, to print the numbers 1 to 10, we can use the following code:

for i := 1; i <= 10; i++ {
    fmt.Println(i)
}

The “while” loop is used to execute a section of code as long as a condition is met. For example, to print the numbers 1 to 10, we can use the following code:

i := 1
while i <= 10 {
    fmt.Println(i)
    i++
}

Conditionals

Conditionals allow us to execute a section of code only if a condition is met. In Go, there are three main types of conditionals: “if”, “else if” and “else”.

The “if” conditional is used to execute a section of code only if a condition is met. For example, to print “Hello” if the variable “number” is greater than 5, we can use the following code:

if numero > 5 {
    fmt.Println("Hi")
}

The “else if” conditional is used to execute a section of code if a different condition is met. For example, to print “Goodbye” if the variable “number” is less than or equal to 5, we can use the following code:

if number > 5 {
    fmt.Println("Hi")
} else if number <= 5 {
    fmt.Println("bye")
}

The “else” conditional is used to execute a section of code if none of the above conditions are met. For example, to print “Bye” if the variable “number” is neither greater than 5 nor less than or equal to 5, we can use the following code:

if number > 5 {
    fmt.Println("Hi")
} else if number <= 5 {
    fmt.Println("Bye")
} else {
    fmt.Println("Ciao")
}

Matrices, Lists and Dictionaries

Arrays, lists and dictionaries are data structures useful for storing and manipulating data.

Arrays are data structures that store a collection of elements of the same type. For example, to create an array of integers called “numbers” containing the numbers 1 to 10, we can use the following code:

numbers := [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Lists are data structures that store a collection of elements of different types. For example, to create a list called “things” containing an integer, a float and a string, we can use the following code:

things := []interface{}{1, 2.3, "hi"}

Dictionaries are data structures that store a collection of elements indexed by a key. For example, to create a dictionary called “people” containing the names of people and their ages, we can use the following code:

persons := map[string]int{
    "John": 20,
    "Mary": 25,
    "Peter": 30,
}

Module Installation

Go comes with a large number of built-in modules, but we can also install external modules. To install an external module, we must first download the source code of the module. Then, we must compile the source code to generate an executable file. Finally, we must run the executable file to install the module.

Code Compilation and Execution

To compile and run code in Go, we must first save the code in a file with the extension “.go”. Then, we must use the “go build” command to compile the code. This will generate an executable file. Finally, we must run the executable file to execute the code.

Compilation for Other Platforms

Go allows us to compile our code for other platforms. To do this, we must use the “go build” command with the “-o” option to specify the name of the executable file. For example, to compile our code for Windows, we can use the following command:

go build -o my_program.exe

Conclusion

In this guide, we’ve covered the basics of Go, from variables to loops, conditionals, arrays, lists and dictionaries, as well as how to install modules and compile for other platforms. If you want to go deeper into Go, there are many online resources you can use to learn more. Good luck!

Leave a Reply