Getting Started with Golang Part I

This article is about getting started with golang!

I started learning go a few months ago, because I wanted to write fast, safe, and performant applications. I began it by reading through An Introduction to Programming in Go by Caleb Doxsey. I’m a thi…


This content originally appeared on DEV Community and was authored by Debabrata Bhattacharya

This article is about getting started with golang!

I started learning go a few months ago, because I wanted to write fast, safe, and performant applications. I began it by reading through An Introduction to Programming in Go by Caleb Doxsey. I'm a third of the way through, and I decided that I wanted to share the joy of learning go with everyone. Without furthe ado, let's get started!

What is golang?

Golang, often shortened to go, is a programming language. It is used to write web servers, client applications, system programs, pretty much anything. It is a great programming language, suited to writing good code effortlessly.

Who uses it?

Google, Uber, Twitch, and others.

What's so great about go?

This article describes it perfectly: https://stackoverflow.blog/2020/11/02/go-golang-learn-fast-programming-languages/ at StackOverflow.

Getting started Part I

In this part, we will install go and run a simple hello world program.

Go to https://golang.org/doc/install and follow the instructions for your operating system.

Now create a folder, lets say goprograms, and open your code editor in it. For this tutorial I will be using VS Code. The folder will be our workspace.

Create a new file helloworld.go, and paste the following code:

package main 

import "fmt"

// This is where program execution starts
func main()  {
    // Prints your name!
    var name string = "User" // Replace this with your name
    message := fmt.Sprintf("Hello world, %v!", name)
    fmt.Println(message)
}

Now, open a terminal in the folder. On VS Code you can open a terminal with Ctrl/CMD + Shift + ~.

Now type go run helloworld.go.

You should see the output in your terminal.

/goprograms on  main [!?] via ? v1.16.5 on ☁️  (us-east-1) 
 go run .\helloworld.go
Hello world, User!

Now, let's break down this program.

package main 

...

This describes a package main, which means a set of related programs which are collectively called main. Here, the name is unique to the context in which it exists, which means there is only one package with a particular name in a single workspace.

Thus, programs in go are related to each other using package names.

...
import "fmt"
...

Here, we see the statement import. This keyword (keywords are words with a special meaning to the go language, and are reserved for use) means that we are importing other packages into our workspace. Packages have names, and the name of the package we are importing is called fmt. By doing this we can use the programs under this package in our programs.

...
func main()  {

}
...

This func keyword signifies a function, which is a discrete block of code that gets executed when called. Here, the name of the function is main, which is a function that gets called first everytime the program runs.

    ...
    var name string = "User" // Replace this with your name
    ...

Here we are creating a variable with the var keyword. Variables are used to store data. We have given the variable a name, name, and it's type is string which is another keyword and means text data can be stored in this variable. We store the string User here.

    ...
    message := fmt.Sprintf("Hello world, %v!", name)
    ...

Here, we are declaring a another variable message. We are assigning it the value on the other side of the :=.

Here, we are using the function fmt.Sprintf() function. It returns a string for us to use.

In the string, we are substituting the name variable in with %v.

    ...
    fmt.Println(message)
    ...

Here, we are printing the value of the variable message using the fmt.Println() function.

That's all for this article, we will go over more in part 2 of this series!

META

This article has been published on


This content originally appeared on DEV Community and was authored by Debabrata Bhattacharya


Print Share Comment Cite Upload Translate Updates
APA

Debabrata Bhattacharya | Sciencx (2021-07-13T19:44:53+00:00) Getting Started with Golang Part I. Retrieved from https://www.scien.cx/2021/07/13/getting-started-with-golang-part-i/

MLA
" » Getting Started with Golang Part I." Debabrata Bhattacharya | Sciencx - Tuesday July 13, 2021, https://www.scien.cx/2021/07/13/getting-started-with-golang-part-i/
HARVARD
Debabrata Bhattacharya | Sciencx Tuesday July 13, 2021 » Getting Started with Golang Part I., viewed ,<https://www.scien.cx/2021/07/13/getting-started-with-golang-part-i/>
VANCOUVER
Debabrata Bhattacharya | Sciencx - » Getting Started with Golang Part I. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/13/getting-started-with-golang-part-i/
CHICAGO
" » Getting Started with Golang Part I." Debabrata Bhattacharya | Sciencx - Accessed . https://www.scien.cx/2021/07/13/getting-started-with-golang-part-i/
IEEE
" » Getting Started with Golang Part I." Debabrata Bhattacharya | Sciencx [Online]. Available: https://www.scien.cx/2021/07/13/getting-started-with-golang-part-i/. [Accessed: ]
rf:citation
» Getting Started with Golang Part I | Debabrata Bhattacharya | Sciencx | https://www.scien.cx/2021/07/13/getting-started-with-golang-part-i/ |

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.