恐慌:Go中的运行时错误:索引超出范围

huangapple go评论143阅读模式
英文:

panic: runtime error: index out of range in Go

问题

我有以下函数,它从终端接收一个命令并根据输入打印一些内容。看起来很简单,如果用户输入'add',系统会打印一行,如果用户没有输入任何内容,它会打印其他内容。

每当用户输入add时,它都能正常工作。如果用户没有输入任何内容,它会抛出GoLang中的运行时错误:索引超出范围。

为什么会这样呢?

  1. func bootstrapCmd(c *commander.Command, inp []string) error {
  2. if inp[0] == "add" {
  3. fmt.Println("你输入了add")
  4. } else if inp[0] == "" {
  5. fmt.Println("你没有输入add")
  6. }
  7. return nil
  8. }
英文:

I have the following function that takes a command from terminal and prints something based on input. It seems simple enough, if the user types 'add' the system prints a line, if the user types nothing, it prints something else.

Whenever the user types add, it works. If the user doesn't type anything it throws

panic: runtime error: index out of range in GoLang

Why is this?

  1. func bootstrapCmd(c *commander.Command, inp []string) error {
  2. if inp[0] == "add" {
  3. fmt.Println("you typed add")
  4. } else if inp[0] == "" {
  5. fmt.Println("you didn't type add")
  6. }
  7. return nil
  8. }

答案1

得分: 37

如果用户没有提供任何输入,inp 数组将为空。这意味着即使索引 0 超出范围,即无法访问 inp[0]

您可以使用 len(inp) 来检查 inp 的长度,然后再检查 inp[0] == "add"。可以像这样进行检查:

  1. if len(inp) == 0 {
  2. fmt.Println("您没有输入 add")
  3. } else if inp[0] == "add" {
  4. fmt.Println("您输入了 add")
  5. }
英文:

If the user does not provide any input, the inp array is empty. This means that even the index 0 is out of range, i.e. inp[0] can't be accessed.

You can check the length of inp with len(inp) before checking inp[0] == "add". Something like this might do:

  1. if len(inp) == 0 {
  2. fmt.Println("you didn't type add")
  3. } else if inp[0] == "add" {
  4. fmt.Println("you typed add")
  5. }

答案2

得分: 12

你首先需要检查inp的长度:

  1. func bootstrapCmd(c *commander.Command, inp []string) (err error) {
  2. if len(inp) == 0 {
  3. return errors.New("no input")
  4. }
  5. switch inp[0] {
  6. case "add":
  7. fmt.Println("你输入了add")
  8. case "sub":
  9. fmt.Println("你输入了sub")
  10. default:
  11. fmt.Println("无效:", inp[0])
  12. }
  13. return nil
  14. }
英文:

You have to check the length of inp first:

  1. func bootstrapCmd(c *commander.Command, inp []string) (err error) {
  2. if len(inp) == 0 {
  3. return errors.New("no input")
  4. }
  5. switch inp[0] {
  6. case "add":
  7. fmt.Println("you typed add")
  8. case "sub":
  9. fmt.Println("you typed sub")
  10. default:
  11. fmt.Println("invalid:", inp[0])
  12. }
  13. return nil
  14. }

答案3

得分: -7

你可以使用recover()来检查切片索引的存在。

  1. func takes(s []string, i int) string {
  2. defer func() {
  3. if err := recover(); err != nil {
  4. return
  5. }
  6. }()
  7. return s[i]
  8. }
  9. if takes(inp, 0) == "add" {
  10. fmt.Println("你输入了 add")
  11. } else {
  12. fmt.Println("你没有输入 add")
  13. }

请注意,这只是代码的翻译部分,不包括任何其他内容。

英文:

Also you can use recover() for check existing index of slices

  1. func takes(s []string, i int) string {
  2. defer func() {
  3. if err := recover(); err != nil {
  4. return
  5. }
  6. }()
  7. return s[i]
  8. }
  9. if takes(inp,0) == "add" {
  10. fmt.Println("you typed add")
  11. } else {
  12. fmt.Println("you didn't type add")
  13. }

huangapple
  • 本文由 发表于 2014年10月1日 01:01:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/26126235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定