Intro to GO lang
Tags: beginners, go, programming, tutorial
Published: 2022-01-26 08:06:35

This is an introductory tutorial (big name) / post on Go lang. I am learning it, so an attempt to share my knowledge.

**Where am I learning?** (In case you are interested)
[Link to the playlist by Hitesh Choudhary](https://youtube.com/playlist?list=PLRAV69dS1uWQGDQoBYMZWKjzuhCaOnBpa)

Before learning anything, the first question should be **WHY?**
And here are few points on **WHY GO lang?**

- To be called as a GOfer.
- Demand and Money (honest answer).

Jokes apart..

- Power.. Yes, it has pointers, we can work in a very low level using go.
- We can build backends of web apps using GO.
- We can optimize cloud utilization using GO. 

**GO specifications**

- It is a compiled language. We compile the files and it gives us an executable file specific to each operating system. (If you are from C or C++ background, this is a bread and butter for you and more on compilation stuff later).
- It is a typed language (We need to specify type of the variables and constants while declaring them).

I hope you are still here after reading the above points. If you are still there...... Let's talk about

**Installation**

Just go the below link and download the installer according to your operating system.
[Download](https://go.dev/dl/)
It's simple in every OS, just next, next, I agree, finish stuff.

**Initialization**

Locate to the location where you want your files to be in terminal and execute the following command to initialize a go package there:

`go mod init <package name>`

In place of package name, we can also provide github repository, but more on it next time.

After running above command, it creates a new file in the directory with name `go.mod` .

**Semi-colons**

GO lexer (which checks syntax and gives error if there any mistakes in syntax) looks for semi-colons.

But if we are using GO extension in VS code and install all the dependencies it requires, we don't need to put any semi-colon and in fact it removes if we put any.

**Hello World**

Now, it's time to write our first program in GO which is obviously going to be Hello World.

Create a file with the name of your choice and I am creating one as `main.go`. 

Obviously, the extension is `.go`.

The first line of every file is going to be:

`package <package name>`

Here the package name generally goes with the name of the file. This is the name with which we export and import the files.

So, now our Hello World program looks like:

```go
package main


```

**Main function**

One important thing to remember is that code execution starts from main function.

The syntax to write any function is:

```go
func function_name(args) {
     //body
}
```

`func` is the keyword here.

So, our Hello World Program looks like:

```go
package main

func main(){

}
```

**Printing to screen**

Final part of our Hello World program is to actually print it to the screen.

The package which is used to do input and output (I/O) operations is **fmt**.

We can use **Println** function to print in a new line or **Print** function to print in the same line or **Printf** function to print a formatted string.

If we observe, all the functions above start with a capital letter. That symbolizes that all those functions are public. This convention applies to variable naming too. So, any time an identifier starts with a capital letter, it's a public identifier.

**Importing a package**

Importing a package is straight forward. We use an import statement and name of package inside double quotes (""):

`import "fmt"`

In case of importing multiple packages:

```go
import (
    "fmt"
    "time"
)
```

"time" is another package which we see in some other post.

Be little cautious with the syntax, no commas (,) in between packages. (I did this mistake first time :) )

Now, our Hello World looks like:

```go
package main

import "fmt"

func main(){
    fmt.Println("Hello World")
}
```

**Output**

Ok, all this is fine. But how do we see the output.

There are 2 ways in which we can see the output.

1. Directly run the program.
2. Create an executable file and run it.

**Running a program**

We can simply use the command `go run <filename>` in the terminal.

In our case, we run `go run main.go` in the terminal to see the output.

**Creating an executable file**

We can create an executable file for any OS from any OS. Exciting right!

The command to create executable file is `GOOS="<OS name>" go build` .

The OS name should be "darwin" for Mac, "windows" for windows and "linux" for linux.

Let's say I am creating executable for windows from windows, then I don't need to specify OS. I just run `go build`.

If I want to create executable for Mac from windows, then I need to specify OS. I have to run `GOOS="darwin" go build`.

**Help**

If need any help in running commands, just execute 
`go help <command name>` in terminal and it shows documentation.


**The End**

That's it for this post. Hope this helps. Next post will be on variables, constants, user input and conversions.