DEV Community

Jorden Williams
Jorden Williams Subscriber

Posted on • Updated on

Let's GO!

NOTE

PATH setup updated 09/09/2024 - this will be tweaked a few more times, I'm sure.

End of Day 1 - Retrospective

Setting up an environment for Golang on a Mac was a little more difficult than I thought it would be. I can't say that I wasn't prepared for a rough start, though. The concepts used with Hugo and blogdown still apply so it's not a "completely from scratch" learning at least.

Building the env

Of course, to use Golang, we need its package. Being on a Mac means we have a few options for acquiring it.

Download

https://go.dev/doc/install will take you to the installer download.
https://github.com/golang/go will get you the source code if you want to put it in a custom directory.
$brew install golang will handle all of it, for you.

If you do decide to go the the brew way, you will need to install homebrew and follow the instructions in your terminal.

Variables

Go needs a few variables, depending on which way you install. I use Homebrew so I'll go over what I had to do to get it working.
export GOPATH="/path/to/go/directory"
I do recommend adding this to your shell profile.

# your executables will go here (go, templ, etc) 
GOBIN="$GOPATH/bin"
[edited export 09/09/2024]
GO_INSTALL="/path/to/<go install [pkg]>"
# prepend path
export PATH="$GOBIN:$GO_INSTALL:${PATH}"
# restart your terminal or source profile to run it now
$ source /path/to/profile/
Enter fullscreen mode Exit fullscreen mode

Testing

Since I'm re-writing my website, I planned on debugging the env while working so I went to a production folder.
mkdir /path/to/project && cd /path/to/project

Go uses a module based system. Simply, the project is a module. Initializing a module works just like any other project, except you include the host name.

$ go mod init github.com/<username>/<module-name>
## the following sets up the go.sum file
## which keeps track of dependencies
## think of it like yarn.lock or cargo.toml
$ go mod tidy
Enter fullscreen mode Exit fullscreen mode

Now open your editor of choice (I use vim, btw) and create a file main.go.

The first line of a *.go file, is the package it belongs to. This is like the hierarchy of a Java program.

## Go
package main
## Java - file structure
import Package.sub.sub.sub...
Enter fullscreen mode Exit fullscreen mode

Go files follow the same principle. This is my current structure.

project
 |-cmd            # functions that get called
 |  |- server     # server side code (routing, etc)
 |  |- api        # allows for external calls (not really needed, atm)
 |
 | -internal      # anything your program loads or consumes
 |  |- views      # Pages (Home, About, Contact) - using templ
 |  |- data       # DB access - I haven't chosen a database yet
 |  |- components # React type components but using templ
 |  |- handlers   # http handlers
 |- go.mod        # cfg file for your project
 |- go.sum        # dependency list and versions
Enter fullscreen mode Exit fullscreen mode

Each sub-directory is its own package.

package server
package api
package views
package data
package components
package handlers
Enter fullscreen mode Exit fullscreen mode

Hello, World!

Now, our favorite program. Hello, World!

package main

import "fmt"

func main(){
    msg := "Hello, World!" // := lets go handle the type
    fmt.Println(msg)
}
Enter fullscreen mode Exit fullscreen mode

Back in your terminal, go run ., and you should see "Hello, World!".
All of the code to needed to run "Hello, World!"

Code for this can be found at my github

Final Comments

Yeah, this took the better part of a day trying to get the right env variables. At one point, I was receiving GOPROXY list is not the empty string, but contains no entries. I did try setting GOPROXY manually, export GOPROXY="sum.golang.org | https://proxy.golang.org" but then I received GOSUMDB missing.

That told me to undo path changes. If you run into any problems, I promise, it is mostly a path issue.

Buy Me A Coffee

Top comments (0)