calc

package module
v0.0.0-...-c63bd75 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2021 License: MIT Imports: 7 Imported by: 0

README

calc

A simple command-line calculator written in Go.

Usage

package main

import (
	"fmt"

	"github.com/xwjdsh/calc"
)

func main() {
	r := calc.Must(calc.Eval("max(1,2,3)+min(4,5,6)*$a+$b", map[string]interface{}{"a": 7, "b": 8.1}))
	fmt.Println(r)
}

Command-lint tool

> cd project_root_dir
> go build ./cmd/calc
>
> ./calc "((1*2-3)+5/4)"
0.25
> ./calc "((1*2-3)+5)/4"
1
> ./calc "1+sin(cos(5))*2"
1.5597467015370547
> ./calc "sum(1,2,3)+pow(2,3-1)"
10
> ./calc -m '{"a":1,"b":2}' '$a+$b'
3
> ./calc -m '{"a":1,"b":2}' '$a+$b'
3
> ./calc '"hello "*2+"world"'
hello hello world
>
Supported Operators
General

+, -, *, /, %, ,

Function

sin, cos, tan, min, max, sum, pow, abs, opp

Bracket

(, )

Licence

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Eval

func Eval(str string, m map[string]interface{}) (interface{}, error)

Eval calculates given expressions.

func Must

func Must(r interface{}, err error) interface{}

Must cause panic if err is not nil.

Types

type Calculator

type Calculator struct {
	// contains filtered or unexported fields
}

Calculator wraps the logic of executing expressions.

func New

func New() *Calculator

New returns a new Calculator instance.

func (*Calculator) Eval

func (c *Calculator) Eval(input string, m map[string]interface{}) (interface{}, error)

Directories

Path Synopsis
cmd
calc command

Jump to

Keyboard shortcuts

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