Brightness Web Server This is the first real program I've written in Go so have at it! I wrote it because I use a tiling window manager that doesn't have any brightness control functionality (at least not that I know of).

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "strconv"
    "strings"
    "os"
)

func main() {

    const BRIGHTNESS_AMOUNT = 25

    maxBrightness := func() int {
        buff, err := ioutil.ReadFile("/sys/class/backlight/gmux_backlight/max_brightness")
        if err != nil {
            log.Fatal(err)
            return -1
        }
        buffInt, err := strconv.Atoi(strings.Replace(string(buff), "\n", "", -1))
        if err != nil {
            log.Fatal(err)
            return -1
        }
        return buffInt
    }()

    currentBrightness := func() int {
        buff, err := ioutil.ReadFile("/sys/class/backlight/gmux_backlight/brightness")
        if err != nil {
            log.Fatal(err)
            return -1
        }
        buffInt, err := strconv.Atoi(strings.Replace(string(buff), "\n", "", -1))
        if err != nil {
            log.Fatal(err)
            return -1
        }
        return buffInt
    }

    http.HandleFunc("/increase", func(w http.ResponseWriter, r *http.Request) {
        brightness := maxBrightness
        if (currentBrightness() + BRIGHTNESS_AMOUNT) &lt; maxBrightness {
                brightness = currentBrightness() + BRIGHTNESS_AMOUNT
        }
        buff := []byte(strconv.Itoa(brightness))
        err := ioutil.WriteFile("/sys/class/backlight/gmux_backlight/brightness", buff, os.ModeDevice)
        if(err != nil) { log.Fatal(err) }
        fmt.Fprintf(w, strconv.Itoa(brightness))
    })

    http.HandleFunc("/decrease", func(w http.ResponseWriter, r *http.Request) {
        brightness := 0
        if (currentBrightness() - BRIGHTNESS_AMOUNT) &gt; 0 {
                brightness = currentBrightness() - BRIGHTNESS_AMOUNT
        }
        buff := []byte(strconv.Itoa(brightness))
        err := ioutil.WriteFile("/sys/class/backlight/gmux_backlight/brightness", buff, os.ModeDevice)
        if(err != nil) { log.Fatal(err) }
        fmt.Fprintf(w, strconv.Itoa(brightness))
    })

    log.Fatal(http.ListenAndServe(":5600", nil))

}
I am also a novice in Go, so can't say anything about the idiomatic of this approach, but some improvements can offer.


Makes no sense to return -1 in currentBrightness/0  because panic is panic.
By the way, Go instead of processing the code of the return value is customary to return the error value as, for example, in strconv.Atoi or ioutil.ReadFile.
Instead of replace better to use TrimSpace

strings.TrimSpace(string(buff))



Code duplication


Not necessary 2 times used strconv.Itoa(brightness).

str := fmt.Sprint(brightness)
checkError(ioutil.WriteFile("brightness", []byte(str), os.ModeDevice))
fmt.Fprintf(w, str)

Since the error handling in Go program a common thing, easy to create a special function.

func checkError(err error) {
    if err != nil {
            log.Fatal(err)
        }
    }



3.
In both handlers have a recurring part: update the value stored in the file. This can also be avoided creating a new function.

update := func(w http.ResponseWriter, brightness int) {
    str := fmt.Sprint(brightness)
    checkError(ioutil.WriteFile("brightness", []byte(str), os.ModeDevice))
    fmt.Fprintf(w, str)
}


Then get as result:

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "os"
    "strconv"
    "strings"
)

func main() {

    const BRIGHTNESS_AMOUNT = 25

    maxBrightness := func() int {
        buff, err := ioutil.ReadFile("max_brightness")
        checkError(err)
        buffInt, err := strconv.Atoi(strings.TrimSpace(string(buff)))
        checkError(err)
        return buffInt
    }()

    currentBrightness := func() int {
        buff, err := ioutil.ReadFile("brightness")
        checkError(err)
        buffInt, err := strconv.Atoi(strings.TrimSpace(string(buff)))
        checkError(err)
        return buffInt
    }

    update := func(w http.ResponseWriter, brightness int) {
        str := fmt.Sprint(brightness)
        checkError(ioutil.WriteFile("brightness", []byte(str), os.ModeDevice))
        fmt.Fprintf(w, str)
    }

    http.HandleFunc("/increase", func(w http.ResponseWriter, r *http.Request) {
        if (currentBrightness() + BRIGHTNESS_AMOUNT) &lt; maxBrightness {
            update(w, currentBrightness()+BRIGHTNESS_AMOUNT)
        } else {
            update(w, maxBrightness)
        }
    })

    http.HandleFunc("/decrease", func(w http.ResponseWriter, r *http.Request) {
        if (currentBrightness() - BRIGHTNESS_AMOUNT) &gt; 0 {
            update(w, currentBrightness()-BRIGHTNESS_AMOUNT)
        } else {
            update(w, 0)
        }
    })

    log.Fatal(http.ListenAndServe(":5600", nil))

}

func checkError(err error) {
    if err != nil {
        log.Fatal(err)
    }
}