Vowlink: A Lightweight Go Functional Programming Library for Simplifying Complex if‑else Logic
Vowlink is a lightweight Go library that introduces Promise‑like functional programming constructs—such as Then, Catch, Finally, Race, and All—to replace tangled if‑else statements, offering chainable calls, error handling, and concurrency control, thereby improving code readability, maintainability, and execution efficiency for backend services.
Lee, a veteran with 17 years in IT, introduces the problem of increasingly complex if-else logic in Go web services built with the Gin framework, which harms readability and maintainability.
To address this, he created vowlink , a lightweight Go library inspired by JavaScript Promise patterns. It provides chainable methods ( Then , Catch , Finally ) and concurrency utilities ( Race , All , Any , AllSettled ) to replace verbose conditional code.
The library defines a PromiseState enum (Pending, Fulfilled, Rejected) and a Promise struct holding state, value, and error. Its API includes Then for success handling, Catch for error handling, Finally for cleanup, and getters GetValue and GetReason .
func UpdateData(c *gin.Context) {
var req struct {
ID int `json:"id"`
Name string `json:"name"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if req.ID == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "id is required"})
return
}
if req.Name == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "name is required"})
return
}
if result, err := db.UpdateName(req.ID, req.Name); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
} else {
c.JSON(http.StatusOK, gin.H{"message": "success", "result": result})
}
}Another example shows a deeply nested if-else chain, illustrating how vowlink can flatten such logic.
func doSomething(data Struct) error {
company := data.Commpay
if company.Name == "hw" {
techGroup := company.TechGroup
if techGroup.Type == "electronic" {
groupType := techGroup.GroupType
if groupType.Level == "A" {
groupResearch := groupType.Research
if groupResearch.Product == "phone" {
proVersion := groupResearch.Version
if proVersion.Version == "1" {
// do something
} else {
// do something
}
} else {
// do something
}
} else {
// do something
}
} else {
// do something
}
}
}The design mirrors JavaScript Promise state machines, defining PromiseState and the Promise struct, and provides methods for chaining and handling results.
const (
// Pending represents the pending state of a promise
Pending PromiseState = iota
// Fulfilled represents the fulfilled state of a promise
Fulfilled
// Rejected represents the rejected state of a promise
Rejected
)Typical usage creates a new promise, resolves or rejects it, and then processes the outcome with Then and Catch :
result := vl.NewPromise(func(resolve func(interface{}, error), reject func(interface{}, error)) {
resolve("hello world", nil)
}).Then(func(value interface{}) (interface{}, error) {
return value.(string) + " vowlink !!", nil
}, func(err error) (interface{}, error) {
return nil, fmt.Errorf("rejected.")
})
fmt.Println("Resolve:", result.GetValue())
result = vl.NewPromise(func(resolve func(interface{}, error), reject func(interface{}, error)) {
reject(nil, fmt.Errorf("error"))
}).Then(func(value interface{}) (interface{}, error) {
return value.(string) + " vowlink", nil
}, func(err error) (interface{}, error) {
return nil, fmt.Errorf("rejected.")
})
fmt.Println("Rejected:", result.GetReason().Error())In summary, vowlink offers a concise, easy‑to‑learn API that transforms complex conditional logic into readable, maintainable, and efficient functional pipelines for Go backend development.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.