Golang: Googles C++ Part 1: IO
JustAWalrus (1179)

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

You are viewing a single comment. View All
HahaYes (1896)

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