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 :
- Go to https://join.replit.com/Hugo (it's my code π)
- Fill in all the details
- Once you've filled all the details verify your accounts by email
- Once your account is verified click on the create button and search Go
- 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 :
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 :
package main
import (
"fmt"
)
And finally you will print your first line of Go, your code should now look like this :
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 :
// 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 :
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 :
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 :
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 :
var world = "world"
Here is a simple program that uses variables :
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 :
if //condition{
}
else if //condition{
}
And for else like this :
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 :
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 :
func
After that you will need to put the function name and brackets and then strage brackets, like this :
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 :
- Addition - In function it'll be Add
- Substraction - In function it'll be sub
- Multiplication - In function it'll be mul
- 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 π
If you've reached this part, congrats. This is the end! Don't forget to follow me on :
If you want to play with me on Lichess, challenge me to a match!
Top comments (0)