By Ugorji Nwoke   15 Jun 2025   /blog   technology golang

Golang Zero Conditional Check

aka contextual IFZERO ? and IFNOTZERO ?^ operators
What:
Reduce verbosity for single statement execution based on zero value comparison
if_compare_x_to_zero_value { exec_single_y_statement }
How?:
Evaluate single expr or return values from a statement evaluation, compare the last value to its zero value and execute RHS conditionally based on its comparison to its zero value.
? __single_statement__ ; if __last_val_on_LHS__ == zeroVal { __single_statement__ }
?^ __single_statement__ ; if __last_val_on_LHS__ != zeroVal { __single_statement__ }
Applies To:
  • values of all types (bool, number, interface, pointer, struct, etc)
  • orthogonally solves 2 very pressing idioms with verbosity concerns: err != nil and v, ok idioms

Example(s)

_, ok = mymap[mykey] if !ok { mymap[mykey] = f2 }
_, ok = mymap[mykey] ? mymap[mykey] = f2
f, err = myfunc(myarg) if err != nil { return “xyz”, nil }
f, err = myfunc(myarg) ?^ return “xyz”, nil
vv, ok := v.(myType); if ok { myFunc(vv) }
vv, ok := v.(myType) ?^ myFunc(vv)
if ts.Mtsptr == nil { ts.Mtsptr = make(map[string]*TestStruc) }
ts.Mtsptr ? ts.Mtsptr = make(map[string]*TestStruc)
// Real example from https://github.com/golang/example/blob/master/ragserver/ragserver-genkit/main.go func main() { ctx := context.Background()
err := googleai.Init(ctx, &googleai.Config{ APIKey: os.Getenv("GEMINI_API_KEY"), }) if err != nil { log.Fatal(err) }
err := googleai.Init(ctx, &googleai.Config{ APIKey: os.Getenv("GEMINI_API_KEY"), }) ?^ log.Fatal(err)
wvConfig := &weaviate.ClientConfig{ Scheme: "http", Addr: "localhost:" + cmp.Or(os.Getenv("WVPORT"), "9035"), }
_, err = weaviate.Init(ctx, wvConfig) if err != nil { log.Fatal(err) }
_, err = weaviate.Init(ctx, wvConfig) ?^ log.Fatal(err)
classConfig := &weaviate.ClassConfig{ Class: "Document", Embedder: googleai.Embedder(embeddingModelName), }
indexer, retriever, err := weaviate.DefineIndexerAndRetriever(ctx, *classConfig) if err != nil { log.Fatal(err) }
indexer, retriever, err := weaviate.DefineIndexerAndRetriever(ctx, *classConfig) ?^ log.Fatal(err)
model := googleai.Model(generativeModelName) if model == nil { log.Fatal("unable to set up gemini-1.5-flash model") }
model := googleai.Model(generativeModelName) ?^ log.Fatal("unable to set up gemini-1.5-flash model")
server := &ragServer{ ctx: ctx, indexer: indexer, retriever: retriever, model: model, } mux := http.NewServeMux() mux.HandleFunc("POST /add/", server.addDocumentsHandler) mux.HandleFunc("POST /query/", server.queryHandler) port := cmp.Or(os.Getenv("SERVERPORT"), "9020") address := "localhost:" + port log.Println("listening on", address)
err = http.ListenAndServe(address, mux)) if err != nil { log.Fatal(err) }
err = http.ListenAndServe(address, mux)) ?^ log.Fatal(err)
}

Impact on Error Handling

Primary concern with error handling:

  1. Verbosity and clutter due to multiple LOC for each error being handled

Concerns with current set of error handling proposals:

  1. silent returns
  2. implicit variables
  3. variable shadowing
  4. new keywords (e.g. try, handle, etc)
  5. new functions
  6. intricate combination with other constructs e.g. named parameters, etc

Benefit of this model:

  1. Simple: no new concepts or nuances to be aware of (chaining, stack traces, etc)
  2. Orthogonal: applies to any value. Errors are just values.
  3. Solve primary concern with error handling and nothing more
  4. Solves verbosity in many contexts (even beyond handling error values) e.g.
    1. error handling ie if err != nil
    2. map contains check ie if v, ok := m[k] OR v = m[k]
    3. type assertion check ie if v, ok = t.(Typ)

Gofmt integration:

  1. Gofmt will not transform one mode to another, respecting author’s choice to use the new operators
Tags: technology golang


Subscribe: Technology
© Ugorji Nwoke