In this very first Lab you will clone the repository of the project to make the setup slightly easier and ensure you can run a simple "Hello world" program.
- Fork this repository to your own personal GitHub account
- Clone your personal copy of the repository into
$HOME/<user>/go/github.com
on your machine and then open the project in your preferred editor. I use Visual Studio Code with the recommended Go extension installed from the marketplace. - You will be using the Go Modules to manage imports in this workshop so in the terminal, ensure you are in the root of the project directory and initialise it. To do this enter the follow commands:
go mod init
In some cases you may be asked to enter the project directory you wish to initialise. If so, the following should work providing you are following the folder structure outlined in Lab 0 and you have not changed the project name after cloning the repository.
go mod init github.com/cloud-hosted-twitter-bot-workshop
Lets start by making a simple Hello-World
program.
Open the main.go
file, located in the cmd
folder and add the code below:
// Define the package - this will help maintain scope in your application
package main
// Define the fmt import for formatting
import "fmt"
func main() {
// Print a formatted string to the terminal
fmt.Println("Hello Fellow Gopher!")
}
Save the file and then run it. To do this, navigate to the directory in a new terminal window and use the command go run cmd/main.go
. This will compile the code and run the program without building a binary (more on this later). The output in the terminal should be Hello Fellow Gopher!
.
In the next lab we will turn this up a notch and turn your simple hello world
program into a web server.
Onto Lab 2