Learn to Code via Tutorials on Repl.it!

← Back to all posts
Golang: Googles C++ Part 1: IO
JustAWalrus (1187)

Golang, or Go if you don't have much time is a language developed by Google as a replacement for C++ and C#. So it includes the good things about C++ and C# whilst ridding the language of the bottlenecks.

Lets look at the structure of a basic Go program:

package main

import (

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

This is about as basic as it gets with Go, lets start at the top.

So what is package main? Well, that tells the compiler that the main runtime function in it is 'main'. That line can help with multi-file programs.

What is that import block? Well just like in C#, C++ and many other languages you can import packages but Go does it in a block.

What is fmt Well it is a package for basic Output!

What is:

func main {

Well that is a function, or a block of code, you can see the 'func' keyword that defines it as a funtion.

Okay, now we can get to Output Streams!

To print a value to the console we import fmt and write out:


Notice there are no semicolons at the end of the lines!

Anyway, let us break this line down:

So the fmt. is telling Go that the name afterwords comes from fmt.

Println is a function in fmt that is able to call an output stream to the console.

Then we have the parentheses. These serve as blocks for passing arguments to the function and or telling it what to do.

In these parentheses we have quotes these tell the function that we want to print a regular string not a string in a variable.

But what is a string?

A string is a data type in Go. Used for storing multiple characters.

Strings can be concatenated meaning that they can be combined.


We could do this


But that gives us Hello World seperated by lines and we don't like that.



So instead we could combine them.

fmt.Println("Hello " + "World")


Hello World

Please do keep in mind that we could just do this:

fmt.Println("Hello World")

And get the same output.

As for input it is a bit more complex.

First of all, we have to add 3 new packages up top.

So do this.

import (

Then we can get to work.

First we define a variable to do our reading.

reader := bufio.NewReader(os.Stdin)

Don't worry about what that means too much because we will be working with os, bufio, and variables soon enough anyway.

Next we do:

text, _ := reader.ReadString('\n')

This way we have a variable called text to work with.

Lastly we do:

text = strings.Replace(text, "\n", "", -1)

Just hardcode it for now, don't think about it too much we
will get into all of this later.

Anyway that is it!

If you want to see the input we can do:


and there ya go!

Anyway if you enjoyed leave an upvote for part 2!

Part 2: https://repl.it/talk/learn/Golang-Googles-C-Part-2-Basic-Variables/42888

Part 3: https://repl.it/talk/learn/Golang-Googles-C-Part-3-Logic-Operations/42926

Part 4: https://repl.it/talk/learn/Golang-Googles-C-Part-4-Functions/42944

Part 5: https://repl.it/talk/learn/Golang-Googles-C-Part-5-For-Loops/43033

HahaYes (1913)

Hey, thanks for beating @CodeLongAndPros for #1 spot. Lemme take that from you real quick

HahaYes (1913)


HahaYes (1913)

@Wuru yeah, but I come out on top

JustAWalrus (1187)

but this is just the beginning! @HahaYes

HahaYes (1913)

@Wuru Currently it is about mine, @CodeLongAndPros @Wuru we all have about a 50% 30% 5% and other market share

HahaYes (1913)

@Wuru hey, but nice tutorial, but I have established myself in the market.

JustAWalrus (1187)

dont forget to upvote :D @HahaYes

HahaYes (1913)

@Wuru of course not! Competitors don't upvote each other's posts

JustAWalrus (1187)

it was a joke you buffoon! @HahaYes

JustAWalrus (1187)

I will also come out on top cuz i have all day to write these @HahaYes

HahaYes (1913)

@Wuru dang, but I have established myself in the market

HahaYes (1913)

@Wuru well I do too but I'm sick

JustAWalrus (1187)

Well, my typing is like sonic @HahaYes

HahaYes (1913)

@Wuru But my posts are longer and better quality?

JustAWalrus (1187)

shush i gotta write part 2 @HahaYes

HahaYes (1913)

@Wuru Hey can you report the next comment to rick roll the repl staff

Kookiez (386)


JustAWalrus (1187)

haha dont forget to upvote :D @OrangeJooce123

DungeonMaster00 (183)

I tested it, and there is a much easier way to capture input

Package: fmt

Function: Scanln


var example string

This saves input to the example variable

awesome10 (234)

ok now i feel like i betrayed hahayes (shh dont tell) for upvoting this

awesome10 (234)

@Wuru i think i got all of them

JustAWalrus (1187)

cool, Part 4 and 5 will come out within the next 2 hours :d @awesome10

ARJPEG (151)

You know, for code blocks you can do something like:

package main

by saying three backticks (```) then the lang and finally ``