Skip to content

Go Language

Go Program Out of Memory

Last week, during on duty, one user reported his program exited silently without any useful information during deployment, the process is aborted with a killed information:

[1]    90616 killed     ./bin/server

This is an intrigue question as why the program is killed by OOM killer instead of a fatal stack overflow error.

Go Reflect: Fail to Set Argument Value During Conversion

This blog records the failure when trying to set the response value using reflect to provide an adaptor for wrapping the original PRC interceptor to the custom interceptor format defined by our tech products.

The difference between them is the custom interceptor moves the response from an argument to a response. This blog introduces the challenges encountered while designing an adaptor.

Signatures of Original RPC Interceptor And Custom Interceptor
// package rpc
// original RPC interceptor signature
type Interceptor func(ctx context.Context, 
    request, response interface{}, processor Processor) uint32
type Processor func(ctx context.Context, request, response interface{}) uint32


// package custom
// interceptor in the tech team
type CustomInterceptor interface {
    Wrap(HandlerFn) HandlerFn
}
type HandlerFn func(ctx context.Context, req interface{}) (interface{}, error)

Go Function Return Values: List, Not Tuple

Recently, I suddenly found that the following code line is weird, how could it pass (int, error) to ...any?

func main
func variadic(a ...any) {
    fmt.Println(a)
    return
}

func array(a []any) {
    fmt.Println(a...)
    return
}

func main() {
    variadic(json.Marshal(""))
    // array(json.Marshal(""))
    // ^ CANNOT PASS COMPILE
}

Symbol Table and Context Value Implementation

After learning briefly about compiler, with the motivation of walker inside astjson, it's a bit compulsory to master some knowledge about symbol table as it helps to understand management of variables/states in a large different scales.

This blog talks first about the symbol table itself, and then check how Go language maintains the values among contexts in src/context. Note that it doesn't talk anything else rather than values in context.

Go Type System and Type Conversion

A Failure of Assignment

Recently, my friend asked me a question, why inthe following go code, b cannot be assigned to f? With knowledges about AST, I got intrigue on it than ever before.

type (
    Bar string
    Foo Bar
)

func main() {
    var f Foo
    var b Bar
    f = b
}
/main.go:17:6: cannot use b (variable of type Bar) as type Foo in assignment
func main() {
    var f Foo = "foo"
    var b Bar
}

The failure is caused by Bar and Foo are not same type, hence, they cannot be assigned to each other. However, assign a string to Bar or Foo directly is valid.

This blog talks why the conversion fails and will introduce the type, type definition, properities of types with some examples.