Home > Backend Development > Golang > How to correctly handle escaped strings in Go?

How to correctly handle escaped strings in Go?

WBOY
Release: 2024-02-05 21:42:13
forward
933 people have browsed it

How to correctly handle escaped strings in Go?

问题内容

我正在创建一个程序,该程序正在处理和计算开源存储库和库的大小,并将数据保存到数据库中以供进一步分析。

  • 我有一个输入字符串:github.com/azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1
  • 解析为格式:github.com/\!azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1
  • 然后我将其解析为 /home/username/dev/glass/tmp/pkg/mod/github.com/\!azure/[email protected] 格式,这是我的文件系统中的有效路径,我在其中已下载该特定的 go 库。
  • 之后,我将该路径传递给 gocloc -程序 (https://github.com/hhatto/gocloc)
  • 并解析结果。

但问题是,当我将字符串 /home/username/dev/glass/tmp/pkg/mod/github.com/\!azure/[email protected] 保存到变量中时,go 实际上添加了对我保存的字符串的另一个转义,因此它实际上是内存中的 /home/username/dev/glass/tmp/pkg/mod/github.com/\\!azure/[email protected] 。 (fmt.println - 例如删除它)

问题是,当我将该字符串作为参数传递给 os/exec(运行 gocloc 和该路径字符串)时,它运行带有两个转义符的命令 - 这不是有效的路径。

有什么办法可以解决这个问题吗?对我来说,一个想法是只创建一个关于我想做的事情的 shell 脚本

此函数将 github.com/azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 解析为 github.com/\!azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a 格式1 - 及之后这被保存到一个变量中,并且该变量比它应该有的多了一次转义。

func parseurltovendordownloadformat(input string) string {
    // split the input string on the first space character
    parts := strings.splitn(input, " ", 2)
    if len(parts) != 2 {
        return ""
    }

    // split the package name on the '/' character
    packagenameparts := strings.split(parts[0], "/")

    // add the '\!' prefix and lowercase each part of the package name
    for i, part := range packagenameparts {
        if hasuppercase(part) {
            packagenameparts[i] = "\\!" + strings.tolower(part)
        }
    }

    // join the modified package name parts with '/' characters
    packagename := strings.join(packagenameparts, "/")

    return strings.replaceall(packagename+"@"+parts[1], `\\!`, `\!`)
}
Copy after login

之后,字符串被解析为以下格式:/home/username/dev/glass/tmp/pkg/mod/github.com/\!azure/[email protected]

传递给此函数:

// alternative gocloc - command.
func linesofcode(dir string) (int, error) {
    // run the `gocloc` command in the specified directory and get the output
    cmd := exec.command("gocloc", dir)
    output, err := cmd.output()
    if err != nil {
        return 0, err
    }

    lines, err := parsetotallines(string(output))
    if err != nil {
        return 0, err
    }

    return lines, nil
}
Copy after login

它使用这个解析函数:

// Parse from the GoCloc response.
func parseTotalLines(input string) (int, error) {
    // Split the input string into lines
    lines := strings.Split(input, "\n")

    // Find the line containing the "TOTAL" row
    var totalLine string
    for _, line := range lines {
        if strings.Contains(line, "TOTAL") {
            totalLine = line
            break
        }
    }

    // If the "TOTAL" line was not found, return an error
    if totalLine == "" {
        return 0, fmt.Errorf("could not find TOTAL line in input")
    }

    // Split the "TOTAL" line into fields
    fields := strings.Fields(totalLine)

    // If the "TOTAL" line doesn't have enough fields, return an error
    if len(fields) < 4 {
        return 0, fmt.Errorf("invalid TOTAL line: not enough fields")
    }

    // Get the fourth field (the code column)
    codeStr := fields[3]

    // Remove any commas from the code column
    codeStr = strings.Replace(codeStr, ",", "", -1)

    // Parse the code column as an integer
    code, err := strconv.Atoi(codeStr)
    if err != nil {
        return 0, err
    }

    return code, nil
}
Copy after login

我尝试过的:

  • 使用 gocloc 作为库,但无法正常工作。
  • 使用单引号而不是转义符,没有让它工作,但我认为可能有一些东西。

解决这个问题的一种方法可能是创建单独的 shell 脚本并将目录作为参数传递给该脚本,并消除那里的转义,我不知道......

如果您想观察所有源代码:https://github.com/haapjari/glass,更具体地说,是文件 https://github.com/haapjari/glass/blob/main/pkg/plugins /goplg/plugin.go 和函数 enrichwithlibrarydata() 和 utils 函数,位于:https://github.com/haapjari/glass/blob/main/pkg/plugins/goplg/utils.go (上面的示例)

有什么想法吗?如何进行?提前致谢!


正确答案


我有一个输入字符串:github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1

解析为格式:github.com/\!azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1

您的解析器似乎有错误。我希望 Azure 成为 !azure:

github.com/!azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1

Go 模块参考

为了避免在不区分大小写的文件系统中提供服务时出现歧义,$module 和 $version 元素进行大小写编码,方法是将每个大写字母替换为感叹号,后跟相应的小写字母。这允许模块 example.com/mexample.com/m 都存储在磁盘上,因为前者被编码为 example.com/!m

The above is the detailed content of How to correctly handle escaped strings in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template