← All Exercises

Go Slice Operations

basicgo~15min

Background

Slices are Go dynamic arrays with length and capacity, requiring careful management of underlying array sharing.

Problem

Write a Go function that takes a slice of integers, filters out negatives, and returns a new sorted slice.

Input

A slice of integers with positive and negative values.

Output

A new sorted slice containing only non-negative integers.

Sample tests

Sample 1

Input: [3, -1, 4, -2, 0, 5]
Expected: [0, 3, 4, 5]

Your solution

Learning objective

Learn slice filtering with append, sorting with sort.Ints, and slice immutability patterns.