Skip to content
← Back to Community
Golang: Googles C++ Part 1: IO
Profile icon
JustAWalrus

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 (
"fmt"
)

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 {
<code>
}

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:

fmt.Println("<str>")

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.

EXAMPLE:

We could do this

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

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

OUTPUT:

Hello World

So instead we could combine them.

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

OUTPUT

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 (
"bufio"
"fmt"
"os"
"strings"
)

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:

fmt.Println(text)

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

Voters
Profile icon
tbystrican
Profile icon
fordguo
Profile icon
zararitz
Profile icon
rileyhead
Profile icon
SarahHeisnoran
Profile icon
FallenAngel13
Profile icon
Verbix
Profile icon
awesome10
Profile icon
Viper2211
Profile icon
mkw
Comments
hotnewtop
Profile icon
JustAWalrus
Profile icon
awesome10

@Wuru omg fast

Profile icon
JustAWalrus

I just wanna beat out hahayes and gain top spot! @awesome10

Profile icon
Coder100
Profile icon
JustAWalrus
Profile icon
JustAWalrus
Profile icon
JustAWalrus
Profile icon
HahaYes

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

Profile icon
CodeLongAndPros

@HahaYes Excuse me?

Profile icon
JustAWalrus

C++ bad @HahaYes

Profile icon
JustAWalrus

its getting heated @CodeLongAndPros

Profile icon
HahaYes

@Wuru WHOA IS THE WHOLE REPL COMMUNITY TEAMING UP ON ME

Profile icon
HahaYes

@Wuru yeah, but I come out on top

Profile icon
JustAWalrus

take that dirty C++! @HahaYes

Profile icon
JustAWalrus

but this is just the beginning! @HahaYes

Profile icon
HahaYes

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

Profile icon
HahaYes

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

Profile icon
JustAWalrus

dont forget to upvote :D @HahaYes

Profile icon
HahaYes

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

Profile icon
JustAWalrus

it was a joke you buffoon! @HahaYes

Profile icon
JustAWalrus

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

Profile icon
HahaYes

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

Profile icon
HahaYes

@Wuru well I do too but I'm sick

Profile icon
JustAWalrus

Well, my typing is like sonic @HahaYes

Profile icon
HahaYes

@Wuru same

Profile icon
HahaYes

@Wuru But my posts are longer and better quality?

Profile icon
JustAWalrus

shush i gotta write part 2 @HahaYes

Profile icon
HahaYes

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

Profile icon
JustAWalrus
Profile icon
Kookiez

WHOA WHOA THE TUTORIAL SECTION IS GETTING COMPETITIVE

Profile icon
JustAWalrus

haha dont forget to upvote :D @OrangeJooce123

Profile icon
fordguo

Great job!

Profile icon
DungeonMaster00

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

Package: fmt

Function: Scanln

Example:

var example string fmt.Scanln(&example)

This saves input to the example variable

Profile icon
[deleted]

So like cin in [email protected]

Profile icon
awesome10

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

Profile icon
JustAWalrus

dont forget part 2! @awesome10

Profile icon
awesome10

@Wuru i think i got all of them

Profile icon
JustAWalrus

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

Profile icon
awesome10
Profile icon
ARJPEG

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

package main {code}

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

Profile icon
JustAWalrus
Profile icon
ARJPEG