August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this section, you will learn how to use the atomic package in an HTTP server environment. The name of the program is atomWWW.go and it will be presented in three parts.
The first part of atomWWW.go is as follows:
package main
import (
"fmt"
"net/http"
"runtime"
"sync/atomic"
)
var count int32
The variable that will be used by the atomic package is a global one, in order to be accessible from anywhere in the code.
The second part of atomWWW.go contains the following Go code:
func handleAll(w http.ResponseWriter, r *http.Request) { atomic.AddInt32(&count, 1) } func getCounter(w http.ResponseWriter, r *http.Request) { temp := atomic.LoadInt32(&count) fmt.Println("Count:", temp) fmt.Fprintf(w, "<h1 align=\"center\">%d</h1>", ...Read now
Unlock full access