How to .go
Tags: programming, go, tutorial, replit
Published: 2023-02-21 17:42:43

## Introduction

Hey people,

have you ever heard about Go? Well, let me tell you that Go is amazing! If you don't know a lot of things about Go, here are some bullet points : 

- statically typed, compiled high-level programming language

- Designed at Google

- syntactically similar to C, but with memory safety, garbage collection, structural typing, and CSP-style concurrency

- It has a mascot which is really cool

- It's very very fast

## Go on Replit

Replit has hundreds of programming languages templates, and when I say hundreds I mean it. There are some Go related templates, for example these : 

- Go 

- Go HTTP Template

- Beego

- Discordgo

These are some popular templates, I actually made Beego! Starting using Go in Replit is very simple, if you don't have account, follow these steps : 

1. Go to https://join.replit.com/Hugo (it's my code :smile:)
2. Fill in all the details
3. Once you've filled all the details verify your accounts by email
4. Once your account is verified click on the create button and search Go
5. Select Go, put a title to your repl and click on the Create Repl button

Once you've clicked on the Create Repl button you will enter to the workspace, where you will be able to start coding!

## Writing your first line of Go

Since I started learning Go, I loving it! First you will need to type this into the main.go file : 

```go
package main
```

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

After that we will import fmt. fmt is a Go package that is used to format basic strings, values, inputs, and outputs. This should be what you have after : 

```go
package main

import (
  "fmt"
)
```

And finally you will print your first line of Go, your code should now look like this :

```go
package main

import (
  "fmt"
)

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

If you're asking yourself why the function main is used, it's because it's the most basic function of the Golang, and its primary purpose is to act as the entry point for the executable code or programs.

You might find it difficult, but I remember all those 9 lines in 30 seconds, and if I can do it, I'm sure you can!

## Comments in Go

Comments in Go are like in JS, just put 2 slashes. Like this :

```go
// This is a comment!
```

## Math Stuff in Go

By Math stuff I mean multiplication, addition... Let's start with some things simple, so you can know how to this things in Go : 

- Multiply

- Divide

- Add

- Substract

This is a program that prints some numbers using all the things I listed above : 

```go
package main

import (
  "fmt"
)

func main() {
  fmt.Println(1*2)
  fmt.Println(1/2)
  fmt.Println(1+2)
  fmt.Println(1-2)
}
```

Very simple!

## Variables in Go

Variables are very easy, to make a variable first you start with this : 

```go
var 
```

As you might have noticed var is short for variable! After you put var, you need to give a name to the variable. Remember that the variable name can't have illigal characters : 

```go
var world 
```

And finally you just have to put and equals and after the equals you will put the value of your variable! Like this : 

```go
var world = "world"
```

Here is a simple program that uses variables : 

```go
package main

import (
  "fmt"
)

func main() {
  var world = "world"
  fmt.Println("Hello", world)
}
```

## If/Else/Else if statements in Go

If, Else and Else if statements on Go are quite simple. They're like a mix of Python and JS If and Else statements. 

To make an if/else if, first you put the if or else if statement and after the condition and then you open the strage brackets : 

```go
if //condition{

}

else if //condition{

}
```

And for else like this : 

```go
else {

}
```

Here is a program that includes an If and Else statement : 

```
package main

import (
  "fmt"
)

func main() {
  var x = 5

  if x == 5{
    fmt.Println("x = 5")
  }else {
    fmt.Println("x does not equal 5")
  }
}
```

Very simple, and now let's make some changes so that if x equals to 3 it will print something! This is the final result : 

```go
package main

import (
  "fmt"
)

func main() {
  var x = 5

  if x == 5 {
    fmt.Println("x = 5")
  } else if x < 6 {
    fmt.Println("x is less than 6")
  } else if x > 6 {
    fmt.Println("x is bigger than 6")
  } else {
    fmt.Println("x does not equal 5 and is not less than or bigger than 6")
  }
}
```

Always remember that the else statement is always the last statement, else can't go before else if!

## Function in Go

And lastly functions in Go. Functions are quite easy, if you know Python, making Go function will be easy for you, as they look quite similar. 

First you say that it's a function, like this : 

```go
func
```

After that you will need to put the function name and brackets and then strage brackets, like this : 

```go
func Hello() {}
```

And finally you put what you want it to do inside the strange brackets, like this : 

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

That was easy!

## The Go Challenge

Now it's your turn, this is the challenge! Make a function that gets 2 numbers that we said in the function, and one of these math methods : 

1. Addition - In function it'll be Add
2. Substraction - In function it'll be sub
3. Multiplication - In function it'll be mul
4. Division - In function it'll be div

The numbers given on the function are intigers and the math method is a string. It might look very simple, but it isn't!

If you finish the challenge, post your answers in the comments!

## The end :wave:

If you've reached this part, congrats. This is the end! Don't forget to follow me on : 

- [Replit](https://replit.com/@Hugo)
- [Use my Replit Join Link](https://join.replit.com/Hugo)
- [Youtube](https://wyoutube.com/@Hugoonreplit)
- [Github](https://github.com/Hugoonreplit)
- [Lichess](https://lichess.org/@/urias6710)

If you want to play with me on Lichess, challenge me to a match!