我是编程新手,并试图用 Go 编写一个简单的普通程序。
package main
import (
"fmt"
"os"
)
var numbers []float64
var sum float64 = 0
func main() {
if len(os.Args) > 1 {
numbers = os.Args[1:]
}
fmt.Println("Numbers are: ", numbers)
for _, value := range numbers {
sum += value
}
}
http://play.golang.org/p/TWNltPO71N
当我构建程序时,出现此错误:
prog.go:15: cannot use os.Args[1:] (type []string) as type []float64 in assignment
[process exited with non-zero status]
那么如何将一段字符串转换为一段浮点数呢?我可以将转换函数映射到切片吗?
精慕HU
跃然一笑
相关分类