// String in between quotes " "
// Variable with var or :=, useful for dealing with values that appear in multiple places in the code, like duplicated use
var task1 = "Watch Golang Course"
// Variables: multiple declarations, at global level if outside a function
task3 = "Create Todolist app"
reward1 = "Reward myself with a smoothie"
taskItems = []string{task1, task2, reward1, task3}
// Slice of strings with []string, list with unlimited items
taskItemsSlice := []string { task1, task2 }
// Slice: add an item with append for slice
append(taskItemsSlice, "My next task")
// Array of items with fixed size [number]<type>
taskItemsFixedLengthArray := [2]string { task1, task2 }
// Declare package for program
// This sentence is a comment
// Main function, entry point of program
// Variable within function and shorter syntax with :=
task2 := "Another task using shorter syntax within a function"
// Print string with newline
// Explicit type default set as 0
// Print with string and variable
fmt.Println("Tasks: ", taskItemsSlice)
// Loop: for with index and item for iteration
for index, task := range taskItems {
// Print with format with Printf with placeholders:
fmt.Printf("%d. %s\n", index+1, task)
// index will start with 0
// Loop: for with index ignored and item for iteration
for _, task := range taskItems {
// Call function with function name and ()
// Call function with parameter inside ()
// Function with func for code reuse and easier reading and maintenance
// Function with argument
func printTasks(taskItems []string) {
fmt.Println("List of Todos")
for index, task := range taskItems {
fmt.Printf("%d. %s\n", index+1, task)
// Function with return value
func addTask(taskItems []string, newTask string) []string {
return append(taskItems, newTask)
// Using net/http to server messages on localhost:8080
fmt.Println("Starting Web API")
// Register handlers when serverhost/ pattern is visited in browser
http.HandleFunc("/", helloUser)
http.HandleFunc("/show-tasks", showTasks)
http.ListenAndServe(":8080", nil)
func showTasks(writer http.ResponseWriter, request *http.Request) {
for index, task := range taskItems {
fmt.Fprintln(writer, index+1, ":", task)