DEV Community

Cover image for How to .go
Hugoonreplit
Hugoonreplit

Posted on

How to .go

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 😄)
  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 :

package main
Enter fullscreen mode Exit fullscreen mode

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"
)
Enter fullscreen mode Exit fullscreen mode

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!")
}
Enter fullscreen mode Exit fullscreen mode

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!
Enter fullscreen mode Exit fullscreen mode

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)
}
Enter fullscreen mode Exit fullscreen mode

Very simple!

Variables in Go

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

var 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Here is a simple program that uses variables :

package main

import (
  "fmt"
)

func main() {
  var world = "world"
  fmt.Println("Hello", world)
}
Enter fullscreen mode Exit fullscreen mode

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{

}
Enter fullscreen mode Exit fullscreen mode

And for else like this :

else {

}
Enter fullscreen mode Exit fullscreen mode

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")
  }
}
Enter fullscreen mode Exit fullscreen mode

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")
  }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

func Hello() {}
Enter fullscreen mode Exit fullscreen mode

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

func Hello() {
  fmt.Println("Hello world")
}
Enter fullscreen mode Exit fullscreen mode

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 👋

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)