How to get the second word of a string in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "c go java c# python c++ rust"

	words := strings.Fields(str)

	if len(words) > 1 {
		fmt.Println("The second word is:", words[1])
	} else {
		fmt.Println("There is no second word.")
	}
}


/*
run:

The second word is: go

*/

 



answered Oct 3, 2024 by avibootz
...