Backend Development 9 min read

Introducing gobox: A Lightweight Go Framework with Modules and Usage Examples

gobox is a self‑developed lightweight Go framework comprising various utility boxes such as color, crypto, exception, gohttp, golog, and gomisc, with detailed explanations of its origin, naming, supported modules, and extensive code examples demonstrating usage for logging, error handling, slice deduplication, file checks, JSON handling, and time utilities.

360 Tech Engineering
360 Tech Engineering
360 Tech Engineering
Introducing gobox: A Lightweight Go Framework with Modules and Usage Examples

gobox is a self‑developed lightweight Go framework that provides a collection of utility packages (called boxes) such as color, crypto, exception, gohttp, golog, gomisc, and others, with the source hosted at https://github.com/goinbox.

The name “gobox” comes from the design where each independent module is a “box”, and the whole collection forms the “gobox”. It evolved from an earlier Go project that implemented a Raft‑based code release system.

Supported boxes include:

├── color          // 为终端输出增加颜色
├── crypto         // 常用加解密相关
├── encoding       // 常用编解码相关
├── exception      // 用errno+msg表示的错误
├── gohttp         // http服务相关
├── golog          // log记录相关
├── gomisc         // 零碎的工具库
├── go-nsq-mate    // 配合操作nsq队列
├── inotify        // 文件系统通知
├── levelcache     // 使用leveldb实现的本地cache
├── mysql          // mysql操作相关
├── page           // 分页操作
├── pool           // 连接池实现
├── redis          // redis操作相关
├── shardmap       // 为减小map锁粒度实现的碎片化map
├── shell          // 执行shell命令相关
├── simplecache    // 内存cache

Each box can be imported as a regular Go package. For example, the color box provides functions to output colored text:

package main

import (
    "github.com/goinbox/color"

    "fmt"
)

func main() {
    fmt.Println(string(color.Black([]byte("Black"))))
    fmt.Println(string(color.Red([]byte("Red"))))
    fmt.Println(string(color.Green([]byte("Green"))))
    fmt.Println(string(color.Yellow([]byte("Yellow"))))
    fmt.Println(string(color.Blue([]byte("Blue"))))
    fmt.Println(string(color.Maganta([]byte("Maganta"))))
    fmt.Println(string(color.Cyan([]byte("Cyan"))))
    fmt.Println(string(color.White([]byte("White"))))
}

The exception package defines errors with an errno and message, offering a simple wrapper around Go’s panic/recover mechanism:

package main

import (
    "github.com/goinbox/exception"

    "fmt"
)

func main() {
    e := exception.New(101, "test exception")

    fmt.Println(e.Errno(), e.Msg())
    fmt.Println(e.Error())
}

Sample output:

101 test exception
errno: 101, msg: test exception

The gomisc box contains many helper functions. Examples include slice deduplication:

package main

import (
    "github.com/goinbox/gomisc"

    "fmt"
)

func main() {
    is := []int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4}
    fmt.Println("origin slice is: ", is)

    is = gomisc.IntSliceUnique(is)
    fmt.Println("after call slice is: ", is)

    ss := []string{"a", "ab", "ab", "abc", "abc", "abc", "abcd", "abcd", "abcd", "abcd", "abcd"}
    fmt.Println("origin slice is: ", ss)

    ss = gomisc.StringSliceUnique(ss)
    fmt.Println("after call slice is: ", ss)
}

Result:

origin slice is:  [1 2 2 3 3 3 4 4 4 4]
after call slice is:  [1 2 3 4]
origin slice is:  [a ab ab abc abc abc abcd abcd abcd abcd abcd]
after call slice is:  [a ab abc abcd]

Other utilities demonstrated are file and directory existence checks, byte slice appending, recursive file listing, JSON file saving and parsing, substring extraction, time‑format constants, a general time layout, and generating random numbers based on the current time.

For instance, checking file existence:

package main

import (
    "github.com/goinbox/gomisc"

    "fmt"
)

func main() {
    f := "/etc/passwd"

    r := gomisc.FileExist(f)
    if r {
        fmt.Println(f, "is exist")
    } else {
        fmt.Println(f, "is not exist")
    }

    d := "/home/ligang/devspace"

    r = gomisc.DirExist(d)
    if r {
        fmt.Println(d, "is exist")
    } else {
        fmt.Println(d, "is not exist")
    }
}

Result:

/etc/passwd is exist
/home/ligang/devspace is exist

Time formatting constants are defined as:

TIME_FMT_STR_YEAR   = "2006"
TIME_FMT_STR_MONTH  = "01"
TIME_FMT_STR_DAY    = "02"
TIME_FMT_STR_HOUR   = "15"
TIME_FMT_STR_MINUTE = "04"
TIME_FMT_STR_SECOND = "05"

Generating a random number from a time value:

package main

import (
    "github.com/goinbox/gomisc"

    "fmt"
    "time"
)

func main() {
    tm := time.Now()
    fmt.Println(gomisc.RandByTime(&tm), gomisc.RandByTime(&tm), gomisc.RandByTime(nil))
}

Output (example):

4423491624236117727 4423491624236117727 1471010178475409526

The article concludes by inviting users to try gobox and provide feedback via the official technical public account.

backendGoUtilitylibraryframeworkExamplegobox
360 Tech Engineering
Written by

360 Tech Engineering

Official tech channel of 360, building the most professional technology aggregation platform for the brand.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.