如何解析输入字符串(例如计算器)以获取运算符和操作数?

I am brand new to the Go programming language and I am trying to build a very simple calculator. The issues I am running into is if someone enters in 4+2 or 5/10 or 100-25 to the command line how do I grab the operator and operands out of this string in order to perform the equation?

This is what I have so far, but this just grabs the entire string

package main

import (
"bufio"
"fmt"
"os"
"stack" //stack code works perfectly
)

func main() {

// Read a line from stdin.
scanner := bufio.NewScanner(os.Stdin)

for scanner.Scan() {
    line := scanner.Text()
    // fmt.Println(line)

    //Separate the string based on operator and operands

    // Push all of the characters onto the stack.
    s := stack.New()
    for i := 0; i < len(line); i++ {
        if err := s.Push(line[i]); err != nil {
            panic("Push failed")
        }
    }

Some pointers for you.

  1. To get operands from your line try strings.FieldsFunc().
  2. Convert your operands to float64 try strconv.ParseFloat() or fmt.Sscanf().
  3. Figure out what operation +, -, /, * you have. Perhaps with strings.ContainsRune().
  4. Perform operation on operands and present results.
  5. Have fun!