August 2019
Beginner to intermediate
798 pages
17h 2m
English
This section will present an application of defer related to logging. The purpose of this technique is to help you organize the logging information of a function in a better way and make it more visible. The name of the Go program that will illustrate the use of the defer keyword in logging is logDefer.go and it will be presented in three parts.
The first part of logDefer.go is as follows:
package main
import (
"fmt"
"log"
"os"
)
var LOGFILE = "/tmp/mGo.log"
func one(aLog *log.Logger) {
aLog.Println("-- FUNCTION one ------")
defer aLog.Println("-- FUNCTION one ------")
for i := 0; i < 10; i++ {
aLog.Println(i)
}
}
The function named one() is using defer to make sure that the second aLog.Println() call is going to ...
Read now
Unlock full access