广告
返回顶部
首页 > 资讯 > 后端开发 > GO >根据循环内的子字符串匹配填充结构
  • 542
分享到

根据循环内的子字符串匹配填充结构

2024-04-05 00:04:21 542人浏览 泡泡鱼
摘要

一分耕耘,一分收获!既然都打开这篇《根据循环内的子字符串匹配填充结构》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后

一分耕耘,一分收获!既然都打开这篇《根据循环内的子字符串匹配填充结构》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新golang相关的内容,希望对大家都有所帮助!

问题内容

如果我们看一下下面的代码,我们如何用从字符串切片中获取的值填充结构变量? https://Go.dev/play/p/kkcpzr5r28w

package main

import (
    "fmt"
    "os"
    "strings"
)

type config struct {
    operation string
    stop      string
    start     string
    file      string
}

func parseconfig(list []string) config {

    var c config
    for _, elem := range list {
        if strings.contains(elem, "op:") {
            sublist := strings.splitaftern(elem, ":", 2)
            c.operation = sublist[1]
        } else if strings.contains(elem, "stop:") {
            sublist := strings.splitaftern(elem, ":", 2)
            c.stop = sublist[1]
        } else if strings.contains(elem, "start:") {
            sublist := strings.splitaftern(elem, ":", 2)
            c.start = sublist[1]
        } else if strings.contains(elem, "file:") {
            sublist := strings.splitaftern(elem, ":", 2)
            c.file = sublist[1]
        }
    }
    return c
}

func main() {

    c := parseconfig(os.args[1:])
    fmt.println(c) // {count the  quick /tmp/file1.txt}
}

使用这些参数调用时,该程序不会返回正确的响应:

go run scan.go op:count start:quick stop:the file:/tmp/file1.txt

我想知道出了什么问题?重构代码来解决问题的最佳方法是什么?


正确答案


希望我已经解决了这个问题,感谢 gophers 社区:Https://go.dev/play/p/u_Dc7ctbsib

package main

import (
    "fmt"
    "strings"
)

type Config struct {
    Operation string
    Stop      string
    Start     string
    File      string
}

func main() {
    list := []string{"op:count", "start:quick", "stop:the", "file:/tmp/file1.txt"}
    fmt.Println(list)
    var c Config
    for _, v := range list {
        if strings.HasPrefix(v, "op:") {
            subList := strings.SplitAfterN(v, ":", 2)
            c.Operation = subList[1]
        } else if strings.Contains(v, "stop:") {
            subList := strings.SplitAfterN(v, ":", 2)
            c.Stop = subList[1]
        } else if strings.Contains(v, "start:") {
            subList := strings.SplitAfterN(v, ":", 2)
            c.Start = subList[1]
        } else if strings.Contains(v, "file:") {
            subList := strings.SplitAfterN(v, ":", 2)
            c.File = subList[1]
        }
    }
    fmt.Println(c) // {count the  quick /tmp/file1.txt}

}



Due to the fact that "stop:the" also wrongly matches "op:",operation 最终设置为“the”而不是“count”。现在问题似乎已经解决,strings.contains 已替换为 strings.hasprefix

好了,本文到此结束,带大家了解了《根据循环内的子字符串匹配填充结构》,希望本文对你有所帮助!关注编程网公众号,给大家分享更多Golang知识!

您可能感兴趣的文档:

--结束END--

本文标题: 根据循环内的子字符串匹配填充结构

本文链接: https://www.lsjlt.com/news/596615.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作