gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 03:33:18 27 4
gpt4 key购买 nike

我不确定我的方法是否正确,或者它是否过于骇人听闻。有没有办法改进这段代码?

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

最佳答案

我认为这样会好一些:

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)。

关于string - Golang 将字符串拆分并解析为不同的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35688070/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com