gost

module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2023 License: MIT

README

gost

GitHub license

Experience the true taste of Rust in Go

Install

go get github.com/myyrakle/gost@{version}

Example

package main

import (
	"math"

	"github.com/myyrakle/gost/pkg/option"
	"github.com/myyrakle/gost/pkg/primitive"
)

func CheckedAdd(a, b primitive.Int) option.Option[primitive.Int] {
	max := primitive.Int(math.MaxInt64)
	if (b > 0 && a > max-b) || (b < 0 && a < max-b) {
		return option.None[primitive.Int]()
	}

	return option.Some(a + b)
}

func main() {
	a := primitive.Int(1)
	b := primitive.Int(2)
	result := CheckedAdd(a, b)

	if result.IsSome() {
		println(result.Unwrap())
	} else {
		println("overflow")
	}

	a = primitive.Int(math.MaxInt64)
	b = primitive.Int(1)
	result = CheckedAdd(a, b)

	if result.IsSome() {
		println(result.Unwrap())
	} else {
		println("overflow")
	}
}

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL