我的索引超出范围,我开始恐慌,我出不去
I've started a panic with my index out of range, and I can't get out
我编写这个程序是为了熟悉类型(不是对象!)。
基本前提是用户输入动物名称(牛、蛇鸟),然后输入动作(吃、移动、发出声音)。然后我的代码查找它并 returns 值。
因此,用户条目应该在一行中,由“ ”分隔。我使用 strings.Split。
当用户只输入一个字符时,我收到 "panic" 通知。我认为这种恐慌是由于编译器试图 "split" 单个字符引起的。
两个问题:
1、我说的对吗?
2. 如何解决?
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
//Create our type object.
type animal struct {
aType, eats, moves, sounds string
}
//Create our methods.
func (animal animal) info (querie string) {
if querie == "eats" {
fmt.Printf("The animal, %s , eats %s\n ", animal.aType, animal.eats)
} else if querie == "moves" {
fmt.Printf("The animal, %s , moves by %s\n ", animal.aType, animal.moves)
} else {
fmt.Printf("The animal, %s , makes the sound %s\n ", animal.aType, animal.sounds)
}
}
func main() {
//Now create our animals
cow := animal{aType:"cow", eats: "grass", moves: "walking", sounds: "moo"}
bird := animal{aType:"bird", eats: "worms", moves: "flying", sounds: "peep"}
snake := animal{aType:"snake", eats: "mice", moves: "slithering", sounds: "hiss"}
// need a boolean to perpetuate our loop
var flag bool = true
for flag {
fmt.Println("Remember enter X to exit")
fmt.Printf(">please enter your (format: type & information) request -> ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
request := scanner.Text()
//Capture user entered data
typed := strings.Split(request, " ")[0]
if typed == "X" {
flag = false
break
}
infoe := strings.Split(request, " ")[1]
// contruct the logic tree.
if !((infoe == "eat") || (infoe == "move") || (infoe == "speak")) {
switch typed {
case "cow":
cow.info(infoe)
case "snake":
snake.info(infoe)
case "bird":
bird.info(infoe)
default:
fmt.Println("I don't know about that animal.")
}
} else {
fmt.Printf("I don't have that informtion")
break
}
}
}
在循环外创建扫描器以避免丢弃缓冲数据。当 Scan() returns 为假时中断。检查并处理无效输入。
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Println("Remember enter X to exit")
if !scanner.Scan() {
break
}
request := scanner.Text()
parts := strings.Split(request, " ")
if parts[0] == "X" {
break
}
if len(parts) < 2 {
fmt.Println("bad input")
break
}
typed := parts[0]
infoe := parts[1]
...
为了简化您的代码,我建议使用 fmt.Scanf
,如下所示:
package main
import "fmt"
func main() {
var animal, action string
fmt.Printf("Enter animal: ")
fmt.Scanf("%s", &animal)
fmt.Printf("Enter action: ")
fmt.Scanf("%s", &action)
fmt.Printf("Animal was %s and action was %s", animal, action)
}
我也不确定为什么有多个反对票。是不是代码写法的问题?如果有人只是想学习这门语言,我认为没关系。先让它工作,然后再专注于其他事情。
一旦您熟悉了这门语言,您就可以完成 Effective Go
中列出的要点
我编写这个程序是为了熟悉类型(不是对象!)。
基本前提是用户输入动物名称(牛、蛇鸟),然后输入动作(吃、移动、发出声音)。然后我的代码查找它并 returns 值。
因此,用户条目应该在一行中,由“ ”分隔。我使用 strings.Split。
当用户只输入一个字符时,我收到 "panic" 通知。我认为这种恐慌是由于编译器试图 "split" 单个字符引起的。
两个问题: 1、我说的对吗? 2. 如何解决?
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
//Create our type object.
type animal struct {
aType, eats, moves, sounds string
}
//Create our methods.
func (animal animal) info (querie string) {
if querie == "eats" {
fmt.Printf("The animal, %s , eats %s\n ", animal.aType, animal.eats)
} else if querie == "moves" {
fmt.Printf("The animal, %s , moves by %s\n ", animal.aType, animal.moves)
} else {
fmt.Printf("The animal, %s , makes the sound %s\n ", animal.aType, animal.sounds)
}
}
func main() {
//Now create our animals
cow := animal{aType:"cow", eats: "grass", moves: "walking", sounds: "moo"}
bird := animal{aType:"bird", eats: "worms", moves: "flying", sounds: "peep"}
snake := animal{aType:"snake", eats: "mice", moves: "slithering", sounds: "hiss"}
// need a boolean to perpetuate our loop
var flag bool = true
for flag {
fmt.Println("Remember enter X to exit")
fmt.Printf(">please enter your (format: type & information) request -> ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
request := scanner.Text()
//Capture user entered data
typed := strings.Split(request, " ")[0]
if typed == "X" {
flag = false
break
}
infoe := strings.Split(request, " ")[1]
// contruct the logic tree.
if !((infoe == "eat") || (infoe == "move") || (infoe == "speak")) {
switch typed {
case "cow":
cow.info(infoe)
case "snake":
snake.info(infoe)
case "bird":
bird.info(infoe)
default:
fmt.Println("I don't know about that animal.")
}
} else {
fmt.Printf("I don't have that informtion")
break
}
}
}
在循环外创建扫描器以避免丢弃缓冲数据。当 Scan() returns 为假时中断。检查并处理无效输入。
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Println("Remember enter X to exit")
if !scanner.Scan() {
break
}
request := scanner.Text()
parts := strings.Split(request, " ")
if parts[0] == "X" {
break
}
if len(parts) < 2 {
fmt.Println("bad input")
break
}
typed := parts[0]
infoe := parts[1]
...
为了简化您的代码,我建议使用 fmt.Scanf
,如下所示:
package main
import "fmt"
func main() {
var animal, action string
fmt.Printf("Enter animal: ")
fmt.Scanf("%s", &animal)
fmt.Printf("Enter action: ")
fmt.Scanf("%s", &action)
fmt.Printf("Animal was %s and action was %s", animal, action)
}
我也不确定为什么有多个反对票。是不是代码写法的问题?如果有人只是想学习这门语言,我认为没关系。先让它工作,然后再专注于其他事情。
一旦您熟悉了这门语言,您就可以完成 Effective Go
中列出的要点