Install Go

Before we can begin writing Go code we’ll need to, would you believe it? Install Go

Install Text Editor

If you already have a text editor of choice, then you can skip this step. If you don’t know what a text editor is, then let me explain.

Software Engineers are modern day wizards.

Goroutines 14

goroutines-14 by Maria Letta- License

How so?

  • We can make updates anywhere in the world ๐ŸŒ to a piece of software and everywhere in the world ๐ŸŒŽ ๐ŸŒ is affected.
  • We can bend reality to our will by making software that can change physics, images ๐ŸŒŒ , voices ๐Ÿ‘น๐Ÿ’ฌ, appearances ๐Ÿ˜ˆ -> ๐Ÿ˜Š
  • We can summon, literally, tens of thousands of computers ๐Ÿค–๐Ÿค–๐Ÿค–๐Ÿค–๐Ÿค–๐Ÿค– to serve us in a moment’s notice.

OK, so programmers are wizards๐Ÿง™โ€โ™‚๏ธ ๐Ÿง™, what’s the point? Well, like anyone who practices the magic arts๐Ÿง๐Ÿงš, you need a tool. Instead of using a wand ๐Ÿช„ developers use text editors or IDEs (Integrated Development Environment). Text editors allow us to use our powers.

Time to choose your Text Editor

1. VSCode

VSCode Gopher

VS Code Gopher by Ashley McNamara- License

2. Vim

Vim Go

Vim Go by Egon Elbre- License

I use Neovim but if you’re a beginner, I highly recommend VSCode. Vim is a very, very powerful ๐Ÿฆธโ€โ™€๏ธ text editor, but it is not beginner friendly. If you know what you’re doing and want to improve your speed and understanding as a developer give Neovim or Vim a try; Here’s an article to get started with Go in Vim or with VSCode you want to search for the Go extension and install it.

Setup

We have Go installed and we have our text editor, now it’s time to start our first projectโ— Are you excitedโ‰๏ธ I am ๐Ÿ˜ From this point on I’ll assume we’re beginners and using VSCode. Open VSCode and hit Ctrl+` (That’s the backtick or the key right next to 1 on a QWERTY keyboard) to open the Terminal and type the following in the terminal:

  1. mkdir basics – make a directory and name it basics (Our project name)
  2. cd basics – change directory into our project basics
  3. go mod init basics – initialize (start) a go module named basics
  4. mkdir cmd – make a directory and name it cmd (Idiomatic to do in Go)
  5. touch cmd/main.go – make a file named main.go in the cmd directory
  6. code -r . – make VSCode update the working directory to our project

Now you should see a little directory named cmd on the left. Open it and click main.go

First Program

Now time to write your first Go code.

package main

import "fmt"

func main() {
	fmt.Println("Hello World!")
}

Lets explain each line.

package main

Go needs you to tell it where it should pack all of the functionality you’re going to make in this file. You can have something like package hello at the top and have many files that have package hello at the top of their files as well. When Go builds it that entire package gets… Well, packaged together ๐Ÿ˜‚ Make sense? In other words grouped or bundled together. Think of noodles ๐Ÿœ being put into a take out box ๐Ÿฅก The files we make are the noodles ๐Ÿœ and the package we declare is the take out box ๐Ÿฅก

package main is special because this is where Go will begin execution of your program. If you’re trying to run a Go app you must have a package main with a func main().

import "fmt"

fmt is a package ๐Ÿ“ฆ! Just like the package main we made one line above, but it comes from the standard library ๐Ÿ“š. The standard library is a place where lots of amazing packages exists, all we have to do is import them! Later we’ll make our own packages and we can run them here in cmd/main.

func main()

func main() is the place where it all begins and it all ends ๐Ÿ, your application that is. Here is where you will start and end anything and everything you want your program to do. So always remember if you want to go run your application, it needs to be in package main and it must be inside of the func main() function.

fmt.Println( ... )

Here we call the fmt package that we imported to use it’s Println function. Println is short for print ๐Ÿ–จ๏ธline. It’s called that because it will print a line of text to your terminal, also known as standard out. You can pass lots of things to Println, but we’ll see more of that in later lessons.

Run your very first program

Save the file and type go run cmd/main.go into your terminal. Playground link

๐Ÿฅณ๐Ÿฅณ Congratulations! ๐ŸŽ‰๐ŸŽŠ๐ŸŽ‰ You’re officially a Gopher!

https://twitter.com/egonelbre

Why not celebrate and Make Your Own Gopher and if you want there’s a very supportive community over on Slack and Reddit and finally Golang Weekly ๐Ÿ“ฐ is great for getting ideas about what’s going on in the Go community.

Problems?

It’s very important that you’re in the basics directory when you run go run cmd/main.go. If you get an error message like: stat main.go: no such file or directory that means you tried to execute something that wasn’t there! go is telling you,

I looked for the file main.go where you asked me to look and it’s not there! So it looks like I can’t run it.

So make sure you’re in the the basics directory, you can find out by typing pwd in your terminal and pressing Enter/Return

If you’re still facing problems send an email to info@gophergo.dev