Golang program to get the first and last element from a slice


In this tutorial, we will discuss how to obtain the first and last element from a slice. A slice is a dynamic-sequence that stores elements of a similar type. It is similar to array but it can be resized whereas an array is of fixed range. Let’s understand the concept using some examples.

Method 1: Using integer values

In this method, we will use integer values to get the first and last element from a slice. The output will be printed on the screen using fmt.Println() function. Let’s go through the example to understand how it can be done.

Algorithm

  • Step 1 − Create a package main and import fmt package in the program.

  • Step 2 − Create a function main and in the function create a slice with name slice_demo and fill it with values from where the first and last element is to be calculated.

  • Step 3 − Fetch the first element from the slice using slice name slice_demo[0], here 0 refers to the first element.

  • Step 4 − Print the first element with the help of fmt.Println() function in Golang, where ln means the next line.

  • Step 5 − Obtain the last element using slice_demo[len(slice_demo)-1] and print it similarly like we did in step4.

Example

Golang program to get the first and last element from a slice using integer values

package main
import "fmt"

// create a function main 
func main() {
   // create a slice and fill it with required elements
   slice_demo := []int{1,2,3,4,5}

   fmt.Println("The demo slice is:")

   fmt.Println(slice_demo)

   // obtain the first element
   first_ele := slice_demo[0]

   fmt.Println("The first element of demo slice is:")

   // print the first element
   fmt.Println(first_ele)

   // obtain the last element using the logic
   last_ele := slice_demo[len(slice_demo)-1]

   fmt.Println("The last element of demo slice is:")
                
   // print the last element
   fmt.Println(last_ele)

}

Output

The demo slice is:
[1 2 3 4 5]
The first element of demo slice is:
1
The last element of demo slice is:
5

Method 2: Using float values

In this method, we will use float values to get the first and last element from a slice. The output will be printed on the screen using fmt.Println() function. Let’s dive deep into the example to see how it works.

Algorithm

  • Step 1 − Create a package main and import fmt package in the program.

  • Step 2 − Create a function main and in the function create a slice with name slice_demo and fill it with integer values from where the first and last element is to be calculated.

  • Step 3 − Fetch the first element from the slice using slice name slice_demo[0], here 0 refers to the first element.

  • Step 4 − Print the first element with the help of fmt.Println() function in Golang, where ln means the next line.

  • Step 5 − Obtain the last element using slice_demo[len(slice_demo)-1] and print it similarly like we did in step4.

Example

Golang program to get the first and last element from a slice using float values

package main
import "fmt"

// create a function main to execute the program
func main() {
            
   // create a slice and fill it with float elements
   slice_demo := []float32{1.8, 3.4, 2.0, 3.9, 4.6}
   fmt.Println("The demo slice is:")
   fmt.Println(slice_demo)
                 
   // fetch the first element using the following logic
   first_ele := slice_demo[0]
   fmt.Println("The first element of demo slice is:")

   //print the first element 
   fmt.Println(first_ele)

   // fetch the last element using the logic here
   last_ele := slice_demo[len(slice_demo)-1]
   fmt.Println("The last element of demo slice is:")

   // print the last element
   fmt.Println(last_ele)

}

Output

The demo slice is:
[1.8 3.4 2 3.9 4.6]
The first element of demo slice is:
1.8
The last element of demo slice is:
4.6

Method 3: Using append method in slice

In this method, we will create a slice using append method and then in that slice we will fetch first and last element. The output will be printed on the screen using fmt.Println() function. Let’s dive deep into the example to see how it works.

Syntax

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

Algorithm

  • Step 1 − Create a package main and import fmt package in the program.

  • Step 2 − Create a function main and in the function create a slice using append function and print the slice on console.

  • Step 3 − Fetch the first element from the slice using slice name slice[0], here 0 refers to the first element.

  • Step 4 − Print the first element with the help of fmt.Println() function in Golang, where ln means the next line.

  • Step 5 − Obtain the last element using slice[len(slice)-1] and print it similarly like we did in step4.

Example

Golang program to get the first and last element from a slice using append method in slice

package main
import "fmt"

func main() {

   var slice []int  // create an empty slice
                
   //append the elements in slice using append function

   slice = append(slice, 1)
   slice = append(slice, 2)
   slice = append(slice, 3)
   slice = append(slice, 4)

   fmt.Println("The demo slice is:")
   fmt.Println(slice)

   // obtain the first element
   first_ele := slice[0]   

   fmt.Println("The first element of demo slice is:")
   fmt.Println(first_ele)

   // obtain the last element
   last_ele := slice[len(slice)-1]

   fmt.Println("The last element of demo slice is:")
   fmt.Println(last_ele)

}

Output

The demo slice is:
[1 2 3 4]
The first element of demo slice is:
1
The last element of demo slice is:
4

Conclusion

We executed the program of finding the first and last element of slice using three methods. In the first example we used integer values in the main function to execute the program. In the second example we used the float values to implement the same and in the third example we used append function. All the examples returned similar outputs. Hence, the program executed successfully.

Updated on: 17-Jan-2023

851 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements