Getting started with Go
By R. S. Doiel, 2025-09-22
This is just a quick tutorial covering installing the Go compiler, tool chain and learning the basics through creating three programs -- hello world, a web server and a Markdown server. It is not a tutorial on the Go language. The Go website has that covered. This is really just the bare minimum to get started with the go command.
Here's the files source files we'll be using in this tutorial
- helloworld.go
- webserver.go
- markdown.go
- markdown_test.go
- handler.go
- handler_test.go
- mdserver's main.go
Installing Go and making sure it works
It covers the basics of how the Go compiler and tool change work by walking throw three Go programs. The first one is to create a hello world program to make sure Go is available and working. The steps will be as follows.
- Download and install Go, see https://go.dev/dl/ (latest is currently 1.25.1)
Creating hello world, making sure Go tool chain is working
- Open a terminal
- create a directory/folder you'll use for you creating a hello world program
- Change into that directory
- Type in the hello world program below and save it as "hello.go"
- Try running "hello.go"
- Follow the instruction to "build" the hello.go program (you're compiling the Go source into an executable)
- Run you newly created executable
Here's the steps we'll take starting with step #3.
mkdir helloworld
cd helloworld
edit hello.go
go run hello.go
go build hello.go
./hello
cd ..
Here's the source code to hello world in Go
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello World!")
}
If you got this far you've created your first Go program.
What are the parts of the source code package to take note of?
package main- Identifies what "package" or module that the source file belongs to. The "main" package is special in that it what represents a stand alone program.
import- This is a statement (including the contents inside the parenthesis) that shows what modules your program relies on.
func- This defines a functions, in this case the main function.
"fmt"- This is the name of the module for formatting text, includes functions like Println that display a line of text.
Things to keep in mind. Go is a typed language. Variables and constants can be declared explicitly or be implicitly used on their first assignment. Scope is block level like Python, TypeScript or ES6 when using "let".
Capitalized functions, variables and constants are "exported" from the current module and can be referenced when that package is later imported. When we use the imported "fmt" package with the "Println" function, the fact that it is capitalized means it can be used in the scope of our package. Other things inside of "fmt" which are not capitalized don't get exported and are not visible inside the current package scope. Capitalization is like the "export" keyword in TypeScript or JavaScript.
Packages and modules are Go's reusable unit of code. Originally Go called them "packages", hence the "package" declaration in our program. There are some technical distinctions. A packages are a collection of Go files in a directory. They don't necessarily carry dependencies in them. This can be an issue for you are importing packages that might break between versions. Modules are implemented using packages but carry the dependency information with them so you compiles are consistent regardless of the latest release of a package that was imported. See the "mod" command below.
Let's look at the go command before looking at the source code. The go command is actually a collection of tools. The commons I used most frequently are the following.
- run
- Run the main package Go file
- build
- This compiles the program into an executable. It can also be used to cross compile if you set some environment variables
- fmt
- Format the source code to a standard set by the Go community
- vet
- Perform analysis on your Go source file(s) and indicate potential issues
We'll use the following two when we build our web server since it'll be constructed as a module.
- mod
- Setup and maintain go module (this is be covered later)
- test
- Test a go module (e.g. files that end in "_test.go")
Build a static web server
We'll be doing the following next.
- Create a new directory for our web server
- Change into it.
- Go to the http package docs, https://pkg.go.dev/net/http#example-FileServer-DotFileHiding
- Copy the example for the dot file hiding file server and save as webserver.go
- Create a hello world index.html file.
- Use Go "run" our static web server
mkdir webserver
cd webserver
edit webserver.go
edit hello.html
go run webserver.go
# You can open your web browser to http://localhost:8080/hello.html
cd ..
Build a Markdown web server
- Create a new directory called "mdserver"
- Change into it
- Initialize the Go module we'll build (e.g. 'github.com/rsdoiel/mdserver', you'd use something that makes sense for you)
- Add a Go modules for working with Markdown, "github.com/yuin/goldmark"
- Write a function to convert Markdown into HTML in a file called markdown.go
- Write a test function called MarkdownToHTML in a file called markdown_test.go
- Run the test to make sure it works
- Write a the function MarkdownHandler function and save it markdown.go
- mkdir called "cli"
- Copy "../webserver/webserver.go" to "cli/mdserver.go"
- Update "cmd/mdserver/mdserver.go" to import our mdserver package
- Wrap the dot file handler function with our MarkdownHandler function
- Using go run to test our web server
mkdir mdserver
cd mdserver
go mod init 'github.com/rsdoiel/mdserver'
go mod tidy
go get "github.com/yuin/goldmark"
edit markdown.go
edit markdown_test.go
go test
# Now we're going to create our handler function and test of it
edit handler.go
edit handler_test.go
go test
# Now let's create our wrapping cli
mkdir -p cmd/mdserver
copy ../webserver/webserver.go cmd/mdserver/main.go
# Import our mdserver package and wrap the dotFileHandler function with the Markdown function
edit cmd/mdserver/main.go
go run cmd/mdserver/main.go
We can make a mdserver executable using the build command and specify an output name.
go build -o bin/mdserver cmd/mdserver/main.go
You've not create Go program that is defined by a module/package "mdserver" and runs that package in a command line program. What's left is to learn the language itself, get familiar with the standard library and write more Go programs.
Next Steps
- Go through some of the Go.dev tutorials to better understand Go
- See Go by Example, Effective Go and Awesome Go
- Familiarize yourself with the standard library
- Try adding a YAML configuration to our two web server example, gopkg.in/yaml.v3
- Try out cross compiling to other computer platforms