小编Jas*_* Gu的帖子

去子串操作,没有索引超出范围?

我想知道为什么第二个fmt.Println中的"s [1:]"没有抛出索引超出范围错误?指数1明显超出范围

package main

import "fmt"

func main() {

   s := "J"

   //fmt.Println(s[1]) // This results in a runtime error: index out of range

   fmt.Println(s[1:]) // Why does this work? Index value 1 is clearly out of range, but the statement prints an empty string

}
Run Code Online (Sandbox Code Playgroud)

go

0
推荐指数
1
解决办法
107
查看次数

标签 统计

go ×1