In my last article, we discussed how to set up your local machine to run and code in GoLang. Today, we will actually start building in GoLang!
Input the following into a main.go file:
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
Let's break down what all the bits and pieces are!
How do we run the code in our project?
- Open Terminal and navigate to the directory containing the
main.go
file. In my case:cd ~/Dev/golang/
- In the terminal:
- Type "
go
" which is a portal into all things go. This command will give us the ability to compile and execute all things GoLang related. - Type "
run
, this will tell the go runtime to excecute and compile what ever file you tell it to next. - Type "
main.go
", this is the file we want to execute. Remember that all GoLang files end with.go
at the end. - Press return and the file should execute and compile.
- Type "
The output should look like this:
Dev/golang/3/ % go run main.go
> Hello World!
Pretty Simple huh?
Let's look at some more of the things the go
command can do.
What are some other Go Commands out of the box?
go build
- Compiles a bunch of go source code files. Does not execute.If you run
go build main.go
nothing will output into your console but if youls
that directory, you will see a new file calledmain
and you can now execute the file using./main
go run
- Complies and executes one or two files, note that it's similar to build but also executes.go fmt
- Formats all the code in each file in the current directorygo test
- Runs any tests associated with the current project
The next two commands are used to handle dependencies in our projects
go get
- Downloads the raw source code of someone else's packagego install
- Compiles and "installs" a package
What does the 'package main' mean?
Package == Project == Workspace
Packages can have many related go files and dependencies contained inside of it, the only requirement for a package is that every file have to state at the top what package they relate to.
Inside of go there is 2 types of packages.
- Executable Packages - Generates a file that we can run (see the go build explainer from the previous section)
- Reusable Packages - Code used as helpers. Great Place to put reusable logic.
How do we know when we are making an Executable or Reusable Package?
- It's a bit tricky, we called the first line of the file
main
which will auto create an executable package called main, anything other package name will not. One requirement of packages named "main" is that we define a function named "main" inside it, which is ran automatically when the program runs. main
is a reserved package name in go.- If you changed the package name to anything else and then ran
go build main.go
, you wouldn't create an executable file.
What does 'import "fmt"' mean?
The import statement is used to give our package (the one we are writing) access to code written inside another package.
- In order for your package to have access to any code outside of it, you have to use import statements.
- You are not limited to packages located in the standard library, you could create your own reusable package.
fmt
is a standard package, you can locate "go standard packages" from https://golang.org/pkg/, this one in particular is here: https://golang.org/pkg/fmt/. This package is usually used to print out things to your terminal.- Standard packages are created or adopted by the core Go team and are vetted, tested, and trustworthy. These can be used to expand the tooling available in go and they have very verbose documentation, which we will lean heavily on.
What is func?
func
is short for "function" which in Go act just like other languages.
We declare a function with the reserved keyword func
then the name of the function then an argument list.
How is the main.go file organized?
We will always organize our main.go file as follows:
1. package main -- package declaration
2. import "fmt" -- import other packages
3. func main(){fmt.PrintIn("Hello World!")} -- Tell go to do something
Drew is a seasoned DevOps Engineer with a rich background that spans multiple industries and technologies. With foundational training as a Nuclear Engineer in the US Navy, Drew brings a meticulous approach to operational efficiency and reliability. His expertise lies in cloud migration strategies, CI/CD automation, and Kubernetes orchestration. Known for a keen focus on facts and correctness, Drew is proficient in a range of programming languages including Bash and JavaScript. His diverse experiences, from serving in the military to working in the corporate world, have equipped him with a comprehensive worldview and a knack for creative problem-solving. Drew advocates for streamlined, fact-based approaches in both code and business, making him a reliable authority in the tech industry.