dongya9346 2019-05-03 15:52
浏览 43
已采纳

替代哈希以处理Go中的混合类型

I'm doing a programming exercise to get familiar with Go. I'm currently writing a parser which parses a string into a command with arguments, e.g:

C w h           Should create a new canvas of width w and height h.
B x y c         Should fill the entire area connected to (x,y) with "colour" c.
Q               Should quit the program.

At first I started using hashes to hold the arguments e.g. w h. But this is inflexible and as you can see c is a colour which will be a string, while the other arguments are integers.

I've started like this:

package main

import (
    "errors"
    "strconv"
    "strings"
)

type command struct {
    id   string
    args map[string]int // Won't work because args can be of mixed types
}

func parseCommand(input string) (command, error) {
    if input == "" {
        return command{}, errors.New("No input")
    }

    commandParts := strings.Split(input, " ")

    switch commandParts[0] {
    case "C":
        if (len(commandParts)) != 3 {
            return command{}, errors.New("C (create) requires 2 arguments")
        }

        w, err := strconv.Atoi(commandParts[1])

        if err != nil {
            return command{}, errors.New("width must be an integer")
        }

        h, err := strconv.Atoi(commandParts[2])

        if err != nil {
            return command{}, errors.New("height must be an integer")
        }

        return command{
            id: "create",
            args: map[string]int{
                "w": w,
                "h": h,
            },
        }, nil
    case "B":
        if (len(commandParts)) != 4 {
            return command{}, errors.New("B (Bucket Fill) requires 3 arguments")
        }

        x, err := strconv.Atoi(commandParts[1])

        if err != nil {
            return command{}, errors.New("x must be an integer")
        }

        y, err := strconv.Atoi(commandParts[2])

        if err != nil {
            return command{}, errors.New("y must be an integer")
        }

        return command{
            id: "bucketFill",
            args: map[string]int{
                "x": x,
                "y": y,
                "c": commandParts[3], // This should be a string!
            },
        }, nil
    case "Q":
        return command{
            id: "quit",
        }, nil
    default:
        return command{}, errors.New("Command not supported")
    }
}

My question is how should I go about parsing an input string into a command, if the arguments I want to return are variable and have mixed types? Thanks.

P.S the commands are free typed in and modify a fake canvas in the terminal e.g:

enter command: C 20 4
----------------------
|                    |
|                    |
|                    |
|                    |
----------------------

// Didn't mention this one but it's a Line if you didn't guess
enter command: L 1 2 6 2
----------------------
|                    |
|xxxxxx              |
|                    |
|                    |
----------------------
  • 写回答

1条回答 默认 最新

  • dongzhuo8210 2019-05-03 16:28
    关注

    Your approach to command isn't right. A command is something you can apply to a canvas. So we say so:

    type canvas struct{ ... }
    
    type command interface {
        apply(canvas *canvas)
    }
    

    Now there are several kinds of commands, each with its own arguments. When being used as a command, however, the caller shouldn't have to care what those arguments are.

    type createCommand struct {
        width  int
        height int
    }
    
    func (c createCommand) apply(canvas *canvas) { ... }
    
    type bucketFillCommand struct {
        x     int
        y     int
        color string
    }
    
    func (c bucketFillCommand) apply(canvas *canvas) { ... }
    
    type quitCommand struct{}
    
    func (c quitCommand) apply(canvas *canvas) { ... }
    

    And then you can parse them (I'd probably pull all the parsing into functions, but this is fine).

    func parseCommand(input string) (command, error) {
        if input == "" {
            return nil, errors.New("No input")
        }
    
        commandParts := strings.Split(input, " ")
    
        switch commandParts[0] {
        case "C":
            if (len(commandParts)) != 3 {
                return nil, errors.New("C (create) requires 2 arguments")
            }
    
            w, err := strconv.Atoi(commandParts[1])
    
            if err != nil {
                return nil, errors.New("width must be an integer")
            }
    
            h, err := strconv.Atoi(commandParts[2])
    
            if err != nil {
                return nil, errors.New("height must be an integer")
            }
    
            return createCommand{width: w, height: h}, nil
        case "B":
            if (len(commandParts)) != 4 {
                return nil, errors.New("B (Bucket Fill) requires 3 arguments")
            }
    
            x, err := strconv.Atoi(commandParts[1])
    
            if err != nil {
                return nil, errors.New("x must be an integer")
            }
    
            y, err := strconv.Atoi(commandParts[2])
    
            if err != nil {
                return nil, errors.New("y must be an integer")
            }
    
            return bucketFillCommand{x: x, y: y, color: commandParts[3]}, nil
        case "Q":
            return quitCommand{}, nil
        default:
            return nil, errors.New("Command not supported")
        }
    }
    

    Note that this return nil as the command when something fails, not command{}.

    Playground

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么