Golang将字符串拆分并解析为不同的类型

时间:2016-02-28 20:48:12

标签: string go split

我不确定我的做法是否正确,或者是否过于苛刻。有没有办法改进这段代码?

func splitStatValues(data string) (uint16, uint16, uint16, uint16, uint16, uint16, uint16, uint32, uint32) {
    vals := strings.SplitN(data, ",", 9)

    var lv, str, agi, stm, spr, wis, con uint16
    var ki, exp uint32

    for _, s := range vals {
        xe := strings.SplitN(s, ":", 2)

        if xe[0] == "Lv" {
            _lv, _ := strconv.ParseUint(xe[1], 10, 16)
            lv = uint16(_lv)
        }

        if xe[0] == "STR" {
            _str, _ := strconv.ParseUint(xe[1], 10, 16)
            str = uint16(_str)
        }

        if xe[0] == "AGI" {
            _agi, _ := strconv.ParseUint(xe[1], 10, 16)
            agi = uint16(_agi)
        }

        if xe[0] == "STM" {
            _stm, _ := strconv.ParseUint(xe[1], 10, 16)
            stm = uint16(_stm)
        }

        if xe[0] == "SPR" {
            _spr, _ := strconv.ParseUint(xe[1], 10, 16)
            spr = uint16(_spr)
        }

        if xe[0] == "WIS" {
            _wis, _ := strconv.ParseUint(xe[1], 10, 16)
            wis = uint16(_wis)
        }

        if xe[0] == "CON" {
            _con, _ := strconv.ParseUint(xe[1], 10, 16)
            con = uint16(_con)
        }

        if xe[0] == "KI" {
            _ki, _ := strconv.ParseUint(xe[1], 10, 32)
            ki = uint32(_ki)
        }

        if xe[0] == "EXP" {
            _exp, _ := strconv.ParseUint(xe[1], 10, 32)
            exp = uint32(_exp)
        }
    }

    return lv, str, agi, stm, spr, wis, con, ki, exp
}

正在解析的字符串是:

Lv:400,STR:9999,AGI:8888,STM:7777,SPR:6666,WIS:5555,CON:4444,KI:3999999999,EXP:1

1 个答案:

答案 0 :(得分:2)

我认为这会稍微好一些:

package main

import (
    "fmt"
    "strconv"
    "strings"
)

type statValues struct {
    lv, str, agi, stm, spr, wis, con uint16
    ki, exp                          uint32
}

func main() {
    fmt.Printf("%+v\n", parseStatValues("Lv:400,STR:9999,AGI:8888,STM:7777,SPR:6666,WIS:5555,CON:4444,KI:3999999999,EXP:1"))
}

func parseStatValues(data string) statValues {
    var sv statValues
    for _, s := range strings.SplitN(data, ",", 9) {
        xe := strings.SplitN(s, ":", 2)
        switch xe[0] {
        case "Lv":
            lv, _ := strconv.ParseUint(xe[1], 10, 16)
            sv.lv = uint16(lv)
        case "STR":
            str, _ := strconv.ParseUint(xe[1], 10, 16)
            sv.str = uint16(str)
        case "AGI":
            agi, _ := strconv.ParseUint(xe[1], 10, 16)
            sv.agi = uint16(agi)
        case "STM":
            stm, _ := strconv.ParseUint(xe[1], 10, 16)
            sv.stm = uint16(stm)
        case "SPR":
            spr, _ := strconv.ParseUint(xe[1], 10, 16)
            sv.spr = uint16(spr)
        case "WIS":
            wis, _ := strconv.ParseUint(xe[1], 10, 16)
            sv.wis = uint16(wis)
        case "CON":
            con, _ := strconv.ParseUint(xe[1], 10, 16)
            sv.con = uint16(con)
        case "KI":
            ki, _ := strconv.ParseUint(xe[1], 10, 32)
            sv.ki = uint32(ki)
        case "EXP":
            exp, _ := strconv.ParseUint(xe[1], 10, 32)
            sv.exp = uint32(exp)
        }
    }

    return sv
}

忽略错误通常是一件坏事,但当然可以确保传递给函数的字符串是有效的(例如,使用RE)。