An open API service indexing awesome lists of open source software.

https://github.com/cwchentw/overloading-golang

Function Overloading in Golang
https://github.com/cwchentw/overloading-golang

function-overloading golang golang-package method-overloading

Last synced: 13 days ago
JSON representation

Function Overloading in Golang

Awesome Lists containing this project

README

          

# Function Overloading in Go

A lightweight argument checker to simulate function overloading in Golang.

## Introduction

Go does not support function or method overloading. One common workaround is using variadic parameters:

```go
func Foo(args ...interface{}) interface{} {
// Process args
}
```

While flexible, this approach sacrifices type safety and often leads to repetitive, error-prone argument checking. This package provides tools to simplify that process.

Another alternative is to use a `map[string]interface{}` for named arguments:

```go
func Bar(arg map[string]interface{}) interface{} {
// Process arg
}
```

This package supports both approaches, offering helpers to validate positional and named arguments.

> ⚠️ Note: Variadic overloading is a powerful but debatable technique in Go. Use it wisely and sparingly.

## Important Notes

* This package performs **runtime validation** using `reflect.TypeOf(...).String()`.
Type names must match Go's reflect string representation (e.g., `"int"`, `"float64"`).

* Rule matching is **order-dependent**.
The first matching rule will be used.

* Many constructors and checks will **panic on invalid input**, including:

* Invalid argument definitions
* Missing required arguments
* No matching rule found

* `NewArgument` enforces strict validation:

* Type must be a non-empty string
* Default value (if provided) must not be `nil`
* Default value must match the declared type

> ⚠️ This package favors simplicity over safety. Use with caution in production code.

## Usage

### 1. List-Based Overloading

Support for multiple type signatures:

```go
package main

import (
ov "github.com/cwchentw/overloading-golang"
"log"
)

var checker *ov.ListChecker

func init() {
checker = ov.NewListChecker(
ov.NewListRule(
ov.NewArgument("int"),
ov.NewArgument("int")),
ov.NewListRule(
ov.NewArgument("float64"),
ov.NewArgument("float64")),
)
}

func main() {
n := add(3, 2).(int)
if n != 5 {
log.Fatal("Incorrect result")
}
}

func add(args ...interface{}) interface{} {
out := checker.Check(args)

switch out[0].(type) {
case int:
return out[0].(int) + out[1].(int)
case float64:
return out[0].(float64) + out[1].(float64)
default:
panic("Unsupported type")
}
}
```

### 2. Optional Arguments

Support for default values (**must match declared type**):

```go
package main

import (
ov "github.com/cwchentw/overloading-golang"
"log"
)

var checker *ov.ListChecker

func init() {
checker = ov.NewListChecker(
ov.NewListRule(
ov.NewArgument("int"),
ov.NewArgument("int", 3)),
)
}

func main() {
n := add(3)
if n != 6 {
log.Fatal("Incorrect result")
}
}

func add(args ...interface{}) int {
out := checker.Check(args)
return out[0].(int) + out[1].(int)
}
```

### 3. Map-Based Overloading

Support for named parameters using maps:

```go
package main

import (
ov "github.com/cwchentw/overloading-golang"
"log"
"reflect"
)

var checker *ov.MapChecker

func init() {
checker = ov.NewMapChecker(
ov.NewMapRule(
"x", ov.NewArgument("int"),
"y", ov.NewArgument("int")),
ov.NewMapRule(
"x", ov.NewArgument("float64"),
"y", ov.NewArgument("float64")),
)
}

func main() {
arg := map[string]interface{}{"x": 3.0, "y": 2.0}
n := add(arg).(float64)
if n != 5.0 {
log.Fatal("Incorrect result")
}
}

func add(arg map[string]interface{}) interface{} {
out := checker.Check(arg)

// Type is determined via reflect.TypeOf(...).String()
switch reflect.TypeOf(out["x"]).String() {
case "int":
return out["x"].(int) + out["y"].(int)
case "float64":
return out["x"].(float64) + out["y"].(float64)
default:
panic("Unsupported type")
}
}
```

## Design Philosophy

This package provides a lightweight and explicit way to simulate function overloading in Go.

It prioritizes:

* Minimal abstraction
* Explicit runtime validation
* Simple rule-based dispatch

It does not attempt to replicate Go's type system or provide compile-time safety.

## License

MIT License
Copyright (c) 2026
Modified and maintained by ByteBard