所以我编写了这个小的 go 程序,它向图灵机发出指令,并从中打印选定的单元格:
package main
import "fmt"
import s "strings"
func main() {
fmt.Println(processturing("> > > + + + . ."));
}
func processturing(arguments string) string{
result := ""
dial := 0
cells := make([]int, 30000)
commands := splitstr(arguments, " ")
for i := 0;i<len(commands);i++ {
switch commands[i] {
case ">":
dial += 1
case "<":
dial -= 1
case "+":
cells[dial] += 1
case "-":
cells[dial] -= 1
case ".":
result += string(cells[dial]) + " "
}
}
return result
}
//splits strings be a delimeter
func splitstr(input, delim string) []string{
return s.Split(input, delim)
}
问题是,当它运行时,控制台不显示任何内容。它只是什么都不显示。我该如何使 fmt.println
从我的函数中得到结果字符串?
最佳答案
表达式
string(cells[dial])
产生整数值 cells[dial]
的 UTF-8 表示。打印带引号的字符串输出以查看发生了什么:
fmt.Printf("%q\n", processturing("> > > + + + . .")) // prints "\x03 \x03 "
我想你想要整数的十进制表示:
strconv.Itoa(cells[dial])
关于arrays - println 在 go 中显示为空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34822405/