Golang监测数组下标是否存在(即PHP isset判断)

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "xyhh_123"
	words := strings.Split(str, "_")
	index := 1
	exists := false
	var subOrderId string
	for i, value := range words {
		if i == index {
			exists = true
			subOrderId = value
			break
		}
	}
	fmt.Println(subOrderId)
	if exists {
		fmt.Printf("下标 %d 存在\n", index)
	} else {
		fmt.Printf("下标 %d 不存在\n", index)
	}

}

你可能感兴趣的:(golang,php)