Creating Slices with `make`
Part of the Fundamentals section of Coddy's GO journey — lesson 76 of 109.
The make function creates slices with a specified length and capacity.
Create a slice of integers with length 3:
numbers := make([]int, 3)
fmt.Println(numbers)This displays:
[0 0 0]Create a slice with length 2 and capacity 5:
scores := make([]int, 2, 5)
fmt.Println(scores, len(scores), cap(scores))This displays:
[0 0] 2 5Challenge
BeginnerIn this challenge, you'll practice creating a slice using the make function. The make function allows you to create a slice with a specific length and capacity.
Your task is to create a slice of strings with a length of 3 and a capacity of 5 using make, then assign some values to it and print the slice.
After printing the slice, you should also print its length and capacity.
Cheat sheet
The make function creates slices with a specified length and capacity.
Create a slice with specified length:
numbers := make([]int, 3)
// Creates [0 0 0]Create a slice with length and capacity:
scores := make([]int, 2, 5)
// Creates slice with length 2, capacity 5Use len() and cap() to check slice properties:
fmt.Println(scores, len(scores), cap(scores))
// Output: [0 0] 2 5Try it yourself
package main
import "fmt"
func main() {
// TODO: Create a slice of strings with length 3 and capacity 5 using make
// var names = ...
// Assign values to the slice
names[0] = "Alice"
names[1] = "Bob"
names[2] = "Charlie"
// Print the slice
fmt.Println("Names:", names)
// Print the length and capacity of the slice
fmt.Printf("Length: %d, Capacity: %d\n", len(names), cap(names))
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code11Composite Types: Slices
Introduction to SlicesDeclaring Slice LiteralsCreating Slices with `make`Slice Length vs CapacityAccessing Slice ElementsAppending ElementsSlicing Existing Slices/ArraysIterating Over SlicesCopying SlicesRecap - Dynamic Lists3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions