Golang分割和解析字符串为不同类型

huangapple go评论85阅读模式
英文:

Golang split and parse a string to different types

问题

我不确定我的方法是否正确,或者是否太过粗糙。有没有办法改进这段代码?

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
英文:

I'm not sure my approach is right or if its too hackish. Is there a way to improve this code?

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
}

The string being parsed is:

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

答案1

得分: 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
}

忽略错误通常是不好的,但是可以通过确保传递给函数的字符串是有效的(例如使用正则表达式)来确保。

英文:

This would be slightly better, I think:

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
}

Ignoring errors is generally a Bad Thing, but it's of course possible to ensure that the string passed to the function is valid (with a RE, for example).

huangapple
  • 本文由 发表于 2016年2月29日 04:48:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/35688070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定