Logo

dev-resources.site

for different kinds of informations.

How to .go

Published at
2/21/2023
Categories
programming
go
tutorial
replit
Author
hugoonreplit
Categories
4 categories in total
programming
open
go
open
tutorial
open
replit
open
Author
12 person written this
hugoonreplit
open
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!

replit Article's
30 articles in total
Favicon
How to use proxy IP to deal with dynamically changing anti-crawler challenges?
Favicon
Calling all Replit users!
Favicon
Baby meets AI: Creating a health tracking 👶 web-app in 15 min with an AI agent 🤖.
Favicon
The Fall of a Community
Favicon
FINDING REPLIT ALTERNATIVE FOR LEARNING CODE
Favicon
I'm come back.
Favicon
Changes to Replit's Starter Plan: Announced Friday
Favicon
Replit Tutorial: How to Code Like a Pro with AI Agents
Favicon
1 chi dars
Favicon
Destiny or Coincidence: Based on Shannon Perry s Experiment.
Favicon
So I completed Replit's free 100 Days of Python course
Favicon
Replit's 100 Days of Code (Days 2-12)
Favicon
AI’s a global affair. But Google’s Project isn’t there?
Favicon
How to make martech POCs easier
Favicon
解決 Replit 猜錯套件安裝的問題
Favicon
@TopDown On Replit
Favicon
The State of Replit
Favicon
Implement SEO Cron Jobs with Replit and SerpApi Code
Favicon
How to .go : For
Favicon
How to .go
Favicon
Building a Mortal Kombat Website
Favicon
What i knew about react!(These are the things that I've found challenging so far on my react journey.)
Favicon
How to code almost every programming language in your phone ?
Favicon
Installing ffmpeg in repl.it
Favicon
CRM app with Node.js, Replit, and MongoDB
Favicon
Building a Discord bot with Node.js and Replit
Favicon
Packaging software with Nix
Favicon
Playing with REMIX
Favicon
Best cloud IDEs?
Favicon
Build a smart contract oracle with Solidity, Node.js, and Replit

Featured ones: