从golang应用发送字符串时出现意外StrComp结果

从golang应用发送字符串时出现意外StrComp结果,go,vbscript,Go,Vbscript,在下面的代码中,我设置了一个ReadString,它读取用户输入并在exec.Command中传递 这很好,但当我尝试将字符串与vbscript中的硬编码字符串进行比较(在本例中,我将其与“hello”进行比较)时,即使用户输入也是“hello”,也总是失败 如果我只是像这样通过命令行运行vbscript cscript.exe script.vbs hello …然后StrComp按预期工作,因此我怀疑这可能是数据类型问题,或者golang应用程序中传递了一些额外的字符 这是主菜单。开始:

在下面的代码中,我设置了一个
ReadString
,它读取用户输入并在
exec.Command
中传递

这很好,但当我尝试将字符串与vbscript中的硬编码字符串进行比较(在本例中,我将其与“hello”进行比较)时,即使用户输入也是“hello”,也总是失败

如果我只是像这样通过命令行运行vbscript

 cscript.exe script.vbs hello
…然后StrComp按预期工作,因此我怀疑这可能是数据类型问题,或者golang应用程序中传递了一些额外的字符

这是主菜单。开始

package main

import (
    "fmt"
    "os/exec"
    "bufio"
    "os"
)

func main() {

    buf := bufio.NewReader(os.Stdin)

    fmt.Print("Type something: ")
    text, err := buf.ReadString('\n')
    if err != nil {
        fmt.Println(err)
    } else {
        args := []string{"./script.vbs", string(text)}
        exec.Command("cscript.exe", args...).Run()
    }
}
下面是脚本.vbs

MsgBox(WScript.Arguments(0))

If StrComp(WScript.Arguments(0), "hello") = 0 Then
    MsgBox("it's the same")
Else
    MsgBox("It's not the same...")
End If

使用windows时,行尾为“\r\n”。我不知道ReadString()是否应该删除分隔符,但即使这样,文本也将包含一个不可见的\r。在保存端使用strings.TrimSpace:

package main

import (
    "fmt"
    "os/exec"
    "bufio"
    "os"
    "strings"
)

func main() {

    buf := bufio.NewReader(os.Stdin)

    fmt.Print("Type something: ")
    text, err := buf.ReadString('\n')
    fmt.Printf("0 got: %T %v %q\r\n", text, text, text)
    text = strings.TrimSpace(text)
    fmt.Printf("1 got: %T %v %q", text, text, text)
    if err != nil {
        fmt.Println(err)
    } else {
        args := []string{"./script.vbs", string(text)}
        exec.Command("cscript.exe", args...).Run()
    }
}
输出(主;使用VBScript MsgBoxes的想象力):


我不熟悉vbscript,但参数[0]不是等于“script.vbs”,而是等于hello吗?您可以尝试添加打印以查看值,因为StrCmp调用可能没有问题。字符串不等于hello。@Ullaakut不,如果您通过cmd
cscript.exe script.vbs hello
运行vbscript,并且您在windows上,您可以看到WScript.Arguments(0)确实是“hello”,您不能按字节打印结果字节吗?我没有windows机器来尝试这个on@Ullaakut我看看我能做些什么,可能需要一段时间,
main
Type something: hello
0 got: string hello
 "hello\r\n"
1 got: string hello "hello"