Standard library / go/constant

go/constant.BinaryOp()

Apply a compatible binary operator. QUO can yield a fraction; QUO_ASSIGN forces integer division. Use Compare or Shift separately. Zero divisors panic. Expected stdout: '7/2 3\n'.

Formal Syntax & Structure

go syntaxgo
func BinaryOp(x_ Value, op token.Token, y_ Value) Value

Parameters & Attributes

NameTypeDescription
x_ValueLeft constant operand.
optoken.TokenOperator token appropriate for this operation.
y_ValueRight constant operand.

Return Value

Value

Apply a compatible binary operator.

Working Example

Example Snippetgo
package main
import (  "fmt"  "go/constant"  "go/token")
func main() {  x := constant.MakeInt64(7)  y := constant.MakeInt64(2)  fmt.Println(constant.BinaryOp(x, token.QUO, y).ExactString(), constant.BinaryOp(x, token.QUO_ASSIGN, y))}
Go Reference