code: func extract(str string) (name string, num int, rest string, ok bool) {
  	if len(str) < 2 || str[0] != '$' {
  		return
  	}
  	brace := false
  	if str[1] == '{' {
  		brace = true
  		str = str[2:]
  	} else {
  		str = str[1:]
  	}
  	i := 0
  	for i < len(str) {
  		rune, size := utf8.DecodeRuneInString(str[i:])
  		if !unicode.IsLetter(rune) && !unicode.IsDigit(rune) && rune != '_' {
  			break
  		}
  		i += size
  	}
  	if i == 0 {
  		// empty name is not okay
  		return
  	}
  	name = str[:i]
  	if brace {
  		if i >= len(str) || str[i] != '}' {
  			// missing closing brace
  			return
  		}
  		i++
  	}
  
  	// Parse number.
  	num = 0
  	for i := 0; i < len(name); i++ {
  		if name[i] < '0' || '9' < name[i] || num >= 1e8 {
  			num = -1
  			break
  		}
  		num = num*10 + int(name[i]) - '0'
  	}
  	// Disallow leading zeros.
  	if name[0] == '0' && len(name) > 1 {
  		num = -1
  	}
  
  	rest = str[i:]
  	ok = true
  	return
  }
docs: // extract returns the name from a leading "$name" or "${name}" in str.
  // If it is a number, extract returns num set to that number; otherwise num = -1.