DEV Community

Cover image for How to .go : For
Hugoonreplit
Hugoonreplit

Posted on

How to .go : For

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!

For

If you've been coding for sometime, I'm sure that you might have come across the for loops in Python. This is a code example using the for loops :

languages = ["go", "css", "c++"]
for x in languages:
  print(x)
Enter fullscreen mode Exit fullscreen mode

For loops are very simple in Python, but I didn't find them so simple in Go! Here is a simple code example of a For loop I did with Go :

package main

import (
    "fmt"
)

func main() {
  i := 3

  for i <= 5{
    fmt.Println("Hello world!")
    i = i + 1
  }
}
Enter fullscreen mode Exit fullscreen mode

Let's look through the code so you can understand For loops! As always we start with the basic stuff :

package main

import (
  "fmt"
)

function main() {
  //blah blah blah
}
Enter fullscreen mode Exit fullscreen mode

The first thing I do is declare a variable named i, and I give it a value of 3. You might be asking yourself why I wrote a colon and an equals instead of an equal. well, this is why :

  • Colon + Equals is for declaration + assignment
  • Equals is for declaration

After doing all that, this is our code :

package main

import (
  "fmt"
)

function main() {
  i := 3
}
Enter fullscreen mode Exit fullscreen mode

And now the real thing starts! First I start the for arguments, so I say :

function main() {
  i := 3

  for i <= 5{
    //blah blah blah
  }
}
Enter fullscreen mode Exit fullscreen mode

Now you might be asking yourself what <= means. In Go it means less than or equal to. After this, you now put the easy stuff inside the for loop. My first line inside the for loop prints Hello world!, and the other one adds 1 to i, and when i is equal to 5 it will stop printing Hello world!

This was the final code :

package main

import (
    "fmt"
)

func main() {
  i := 3

  for i <= 5{
    fmt.Println("Hello world!")
    i = i + 1
  }
}
Enter fullscreen mode Exit fullscreen mode

And this was the result of the console :

Hello world!
Hello world!
Hello world!
Enter fullscreen mode Exit fullscreen mode

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)