Get Started with Go!

Installation

I use an Arch based Linux distribution so I am going to install golang through pacman. If you are a Mac or a Windows user you can see the installation procedure over here.

Updated the packages

$ sudo pacman -Syu

Instal…


This content originally appeared on DEV Community and was authored by Aarush Goyal

Installation

I use an Arch based Linux distribution so I am going to install golang through pacman. If you are a Mac or a Windows user you can see the installation procedure over here.

Updated the packages

$ sudo pacman -Syu

Install golang

$ sudo pacman -Sy go

Testing if the installation was successful

$ go version

it should give us an output like this :

go version go<version number> linux/amd64

Setting Up the Code Editor

Make a directory and run go mod init github.com/<github username>/<repo name>

This will create a go.mod file in the working directory.

If you are a JS developer you will be familiar to the npm init command that creates a package.json in the working directory to store the information about all the dependencies that the npm package would use. go mod init is similar to that and works in a similar way.

Open the working directory in Visual Code by running the command code .

Download Go extension from the Visual Studio Code Marketplace

You will see a pop up notification in Visual Studio Code like this:

Get%20Started%20with%20Go!%20067d2ddd6ff240edaba2b303ef59c705/Untitled.png

Click on Install all and wait some time.

After the process are completed we can now get to writing your first few lines in go.

For a video tutorial till now you can check this link.

Hello World

Hello world is pretty much the first line of code that most programmers write while learning a new language so how can we miss this tradition.

Make a new file called main.go and write the following lines of code :

https://media.giphy.com/media/13HgwGsXF0aiGY/giphy.gif

package main

import "fmt"

func main() {
    fmt.Println("Hello world")
}

Lets take a look at what we have written.

The package main tells the Go compiler that the package should compile as an executable program instead of a shared library.

The main function in the package main will be the entry point of our executable program. When you build shared libraries, you will not have any main package and main function in the package.

We can import modules using the import command like in the Hello World program we just wrote we imported the fmt library.

fmt library is very similar to the print commands in python or console.log in java-script.

fmt.Println() displays the data inside it in the terminal.

To get more information about fmtmodule click here.

Executing our code

To run our code we can use go run main.go command in the terminal.

We can also compile the go code into a executable binary file by using go build main.go

You can then execute the file by ./main

$ go run main.go
Hello world

$ go build main.go

$ ls
go.mod main.go main

$ ./main
Hello world

You are all set for learning Go!

https://media.giphy.com/media/8YHmc8luwmJJjFY7zh/giphy.gif


This content originally appeared on DEV Community and was authored by Aarush Goyal


Print Share Comment Cite Upload Translate Updates
APA

Aarush Goyal | Sciencx (2021-05-30T17:57:56+00:00) Get Started with Go!. Retrieved from https://www.scien.cx/2021/05/30/get-started-with-go/

MLA
" » Get Started with Go!." Aarush Goyal | Sciencx - Sunday May 30, 2021, https://www.scien.cx/2021/05/30/get-started-with-go/
HARVARD
Aarush Goyal | Sciencx Sunday May 30, 2021 » Get Started with Go!., viewed ,<https://www.scien.cx/2021/05/30/get-started-with-go/>
VANCOUVER
Aarush Goyal | Sciencx - » Get Started with Go!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/30/get-started-with-go/
CHICAGO
" » Get Started with Go!." Aarush Goyal | Sciencx - Accessed . https://www.scien.cx/2021/05/30/get-started-with-go/
IEEE
" » Get Started with Go!." Aarush Goyal | Sciencx [Online]. Available: https://www.scien.cx/2021/05/30/get-started-with-go/. [Accessed: ]
rf:citation
» Get Started with Go! | Aarush Goyal | Sciencx | https://www.scien.cx/2021/05/30/get-started-with-go/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.