/rush

Rust in Go = Rush

Primary LanguageGoApache License 2.0Apache-2.0

rush

Rust in Go = Rush

Install

go get github.com/chiro-hiro/rush

Usage

Ordinary code

package main

func findValue[T int32 | float32](slices []T, search T) (T, error) {
  for _, slice := range slices {
    if slice == search {
      return slice, nil
    }
  }
  return 0, errors.New("not found")
}

func main(){
  slice := []int32{3, 4, 5, 6, 8, 9, 11}
  find, err := findValue(slice, 8)
  if err != nil {
    println(err.Error())
  } else {
    println(find)
  }
}

Rewrite with rush

package main

import (
  "errors"

  "github.com/chiro-hiro/rush/option"
  "github.com/chiro-hiro/rush/result"
)

func findValueWithResult[T int32 | float32](slices []T, search T) result.Result[T] {
  for _, slice := range slices {
    if slice == search {
      return result.Ok(slice)
    }
  }
  return result.Err[T](errors.New("not found"))
}

func main() {
  slice := []int32{3, 4, 5, 6, 8, 9, 11}
  findWResult := findValueWithResult[int32](slice, 8).Unwrap()
  println(findWResult)
}

Wrap go function

You can wrap Go function with result.From[T] and option.From[T]

findWrapResult := result.From(findValue[int32](slice, 8))
println(findWrapResult.Unwrap())

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details

built with ❤️