×

Go Tutorial

Go Basics

Go Variables

Go Literals

Go Type Handling

Go Operators

Go Decision Making

Go Loops

Go Functions

Go String

Go Arrays

Go Slices

Go Maps

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang append() Function with Examples

Golang | append() Function: Here, we are going to learn about the built-in append() function with its usages, syntax, and examples.
Submitted by IncludeHelp, on October 13, 2021 [Last updated : March 15, 2023]

append() Function

In the Go programming language, the append() is a built-in function that is used to append elements to the end of a slice and returns an updated slice.

It accepts two or more parameters (slice []Type, elems ...Type) and returns an updated slice.

Note: If it has sufficient capacity, the destination is resliced to accommodate the new elements. If it does not, a new underlying array will be allocated.

Syntax

func append(slice []Type, elems ...Type) []Type

Parameter(s)

  • slice : Slice in which we have to append the elements.
  • Elems... : One or more elements to append.

Return Value

The return type of the append() function is a []Type, it returns the updated slice.

Example 1

// Golang program to demonstrate the
// example of append() function

package main

import (
	"fmt"
)

func main() {
	// Creating int and string slices
	s1 := []int{10, 20, 30}
	s2 := []string{"Hello", "World"}

	// Printing types and values of slices
	fmt.Printf("%T, %v\n", s1, s1)
	fmt.Printf("%T, %q\n", s2, s2)

	// Appending the elements
	s1 = append(s1, 40, 50)
	s2 = append(s2, "How are you?", "Boys")

	// After appending,
	// Printing types and values of slices
	fmt.Println("After appending...")
	fmt.Printf("%T, %v\n", s1, s1)
	fmt.Printf("%T, %q\n", s2, s2)
}

Output

[]int, [10 20 30]
[]string, ["Hello" "World"]
After appending...
[]int, [10 20 30 40 50]
[]string, ["Hello" "World" "How are you?" "Boys"]

Example 2

// Golang program to demonstrate the
// example of append() function

package main

import (
	"fmt"
)

func main() {
	// Creating int and string slices
	s1 := []int{10, 20, 30}
	s2 := []string{"Hello", "World"}

	// Creating slices to append
	slice1 := []int{40, 50}
	slice2 := []string{"How're you?", "Boys"}

	// Printing types and values of slices
	fmt.Printf("%T, %v\n", s1, s1)
	fmt.Printf("%T, %q\n", s2, s2)

	// Appending the slices
	s1 = append(s1, slice1...)
	s2 = append(s2, slice2...)

	// After appending,
	// Printing types and values of slices
	fmt.Println("After appending...")
	fmt.Printf("%T, %v\n", s1, s1)
	fmt.Printf("%T, %q\n", s2, s2)
}

Output

[]int, [10 20 30]
[]string, ["Hello" "World"]
After appending...
[]int, [10 20 30 40 50]
[]string, ["Hello" "World" "How're you?" "Boys"]

Example 3

// Golang program to demonstrate the
// example of append() function
// Appending string to a byte slice

package main

import (
	"fmt"
)

func main() {
	// Creating a byte slice
	s := []byte("Hello ")

	// Printing type and value of slice
	fmt.Printf("%T, %q\n", s, s)

	// Appending the string to byte slice
	s = append(s, "How're you?"...)

	// After appending,
	// Printing type and value of slice
	fmt.Println("After appending...")
	fmt.Printf("%T, %q\n", s, s)
}

Output

[]uint8, "Hello "
After appending...
[]uint8, "Hello How're you?"

Golang builtin Package »



Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.