How to modify the local version of Linux kernel?

Execute “make menuconfig” command, then select “General setup” -> “Local version - append to kernel release“. Add you preferred name, e.g.: “.nan“, then save it.

Check if it is saved in .config file successfully:

[root@linux]# grep -i ".nan" .config
CONFIG_LOCALVERSION=".nan"

After making sure it is saved successfully, you can execute “make” command.

Fix “unsupported protocol scheme” issue in golang

When using “http” package in golang, you should use the full domain name, For example:

package main
import (
    "net/http"
    "fmt"
    "io/ioutil"
    "os"
)

func main() {
    resp, err := http.Get("www.google.com")
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    text, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    fmt.Println(string(text))
}

Executing it, the error will be:

Get www.google.com: unsupported protocol scheme ""

Add “http://” before “www.google.com“, then executing it. The result is OK:

<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta co.... 

P.S., the full code is here.

Use sync.WaitGroup in Golang

sync.WaitGroup provides a goroutine synchronization mechanism in Golang, and is used for waiting for a collection of goroutines to finish.

The sync.WaitGroup structure is like this:

type WaitGroup struct {
    m       Mutex
    counter int32
    waiters int32
    sema    *uint32
}

There is a counter member which indicates how many goroutines need to be waited are living now.

sync.WaitGroup also provides 3 methods: Add, Done and Wait. Add method is used to identify how many goroutines need to be waited. When a goroutine exits, it must call Done. The main goroutine blocks on Wait, Once the counter becomes 0, the Wait will return, and main goroutine can continue to run.

Let’s see an example:

package main

import (
    "sync"
    "time"
    "fmt"
)

func sleepFun(sec time.Duration, wg *sync.WaitGroup) {
    defer wg.Done()
    time.Sleep(sec * time.Second)
    fmt.Println("goroutine exit")
}

func main() {
    var wg sync.WaitGroup

    wg.Add(2)
    go sleepFun(1, &wg)
    go sleepFun(3, &wg)
    wg.Wait()
    fmt.Println("Main goroutine exit")

}

Because the main goroutine need to wait 2 goroutines, so the argument for wg.Add is 2. The execution result is like this:

goroutine exit
goroutine exit
Main goroutine exit

Please notice, the Add must go ahead of Done. For detailed inforamtion, you can refer this link: Example for sync.WaitGroup correct?.

P.S., the full code is here.