英文:
Command Line Arguments after EXE
问题
我有我的脚本"file.go"通过"go build file.go"构建,现在我有了"file.exe"。
在代码中,我有"steamid := xxxxxxxxxxxxxxxx"。有没有办法在命令提示符中执行"file.exe"时使用"file.exe -steamid=xxxxxxxxxxxxxxxx"?
代码:
package main
import (
"crypto/md5"
"fmt"
)
func main() {
steamid := xxxxxxxxxxxxxxxx
h := md5.New()
h.Write([]byte("BE"))
for i := 0; i < 8; i++ {
h.Write([]byte{byte(steamid & 0xFF)})
steamid >>= 8
}
fmt.Printf("Battleye GUID: %x", h.Sum(nil))
}
我已经做到了这一步,有新的回复:
package main
import (
"crypto/md5"
"fmt"
"bufio"
"os"
"flag"
)
var SteamID string
func init() {
flag.StringVar(&SteamID, "steamid", "XXXXXXXXXXXXXXXXX", "17位数字的SteamID")
}
func main() {
steamid := &SteamID
h := md5.New()
h.Write([]byte("BE"))
for i := 0; i < 8; i++ {
h.Write([]byte{byte(*steamid & 0xFF)})
*steamid >>= 8
}
fmt.Printf("Battleye GUID: %x", h.Sum(nil))
fmt.Print("\n按下'Enter'继续...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
错误:
C:\Go\bin>go build file.go
# command-line-arguments
.\file.go:24: invalid operation: steamid & 255 (类型不匹配 *string 和 int)
.\file.go:25: invalid operation: steamid >>= 8 (类型 *string 不能进行位移操作)
<details>
<summary>英文:</summary>
I have my script "file.go" Built with "go build file.go" now I have "file.exe"
In the code I have "steamid := xxxxxxxxxxxxxxxx" Is there anyway when executing file.exe in cmd like "file.exe -steamid=xxxxxxxxxxxxxxxx"
code:
package main
import (
"crypto/md5"
"fmt"
)
func main() {
steamid := xxxxxxxxxxxxxxxx
h := md5.New()
h.Write([]byte("BE"))
for i := 0; i < 8; i++ {
h.Write([]byte{byte(steamid & 0xFF)})
steamid >>= 8
}
fmt.Printf("Battleye GUID: %x", h.Sum(nil))
}
----
I've gotten as far as here with new replys;
package main
import (
"crypto/md5"
"fmt"
"bufio"
"os"
"flag"
)
var SteamID string
func init() {
flag.StringVar(&SteamID, "steamid", "XXXXXXXXXXXXXXXXX", "17 Numbers SteamID")
}
func main() {
steamid := &SteamID
h := md5.New()
h.Write([]byte("BE"))
for i := 0; i < 8; i++ {
h.Write([]byte{byte(steamid & 0xFF)})
steamid >>= 8
}
fmt.Printf("Battleye GUID: %x", h.Sum(nil))
fmt.Print("\nPress 'Enter' to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
Error:
C:\Go\bin>go build file.go
# command-line-arguments
.\file.go:24: invalid operation: steamid & 255 (mismatched types *string and int)
.\file.go:25: invalid operation: steamid >>= 8 (shift of type *string)
</details>
# 答案1
**得分**: 2
标准库中包含的[flag包][1]可以实现这一功能。
在你的脚本中需要添加以下内容:
```go
var SteamID string
func init() {
flag.StringVar(&SteamID, "steamid", "<插入默认值>", "<插入帮助文本>")
}
(如果你需要将其作为整数获取,可以使用[Int64Var][2])
然后在你的主函数中添加:
flag.Parse()
这将初始化`SteamID`的值
[1]: http://golang.org/pkg/flag/
[2]: http://golang.org/pkg/flag/#Int64Var
英文:
the flag package included in the standard library does just that.
what you need to add in your script:
<!-- language: go -->
var SteamID string
func init() {
flag.StringVar(&SteamID, "steamid", "<insert default value>", "<insert help text>")
}
(in case you need to get it as an integer, use Int64Var instead)
then in your main function add:
flag.Parse()
This will initialise the value of SteamID
答案2
得分: 0
错误信息中已经说明了问题所在。你不能对字符串、指向字符串的指针或任何非整数类型进行位操作,你需要先将它们转换或解析为整数。使用strconv
包中的strconv.ParseInt及其相关函数来解析字符串。
parsedID, e := strconv.ParseInt(*steamID, 16, 64)
if e != nil { log.Fatal("无法解析ID") }
// 使用parsedID。
英文:
It's all in the error message. You can't do bitwise operations with strings, pointers to strings or anything that is not an integer, you need to convert or parse them into integers first. Use strconv.ParseInt and its friends from the strconv
package to parse strings.
parsedID, e := strconv.ParseInt(*steamID, 16, 64)
if e != nil { log.Fatal("couldn't parse the ID") }
// Use parsedID.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论