How to get the last two array elements in Go

1 Answer

0 votes
package main

import "fmt"

func main() {
	arr := [...]string{"go", "java", "php", "c", "c++", "python", "c#"}

	fmt.Printf("%v\n", arr[len(arr) - 2:len(arr)])
	
	lasttwo := arr[len(arr) - 2:len(arr)]
	
	fmt.Printf("%v\n", lasttwo)
	fmt.Printf("%v\n", lasttwo[0])
	fmt.Printf("%v\n", lasttwo[1])
}
   
   
   
/*
run:
   
[python c#]
[python c#]
python
c#
 
*/

 



answered Aug 10, 2020 by avibootz
...