Slices and maps in Go

Learn Go · Beginner · 11 min · After Variables, types and constants in Go

How do you keep a list or a lookup table in Go?

  1. 1Guess

    package main
    
    import "fmt"
    
    func main() {
    	a := []int{1, 2, 3}
    	b := a
    	b[0] = 9
    	fmt.Println(a[0])
    }
    
    What does this program print?
  2. 2Read

    Slices

    A slice is a view on an array. It has a length, len(s), and a capacity, cap(s). append(s, x) returns a slice that holds the extra element. It may use the same array or a new, larger one, so always keep the result. s[1:3] is a sub-slice: it shares the array with s and holds elements 1 and 2.

  3. 3Code

    Build a slice with append

    1 / 2
    package main
    
    import "fmt"
    
    func main() {
    	var s []int
    	for i := 1; i <= 5; i++ {
    		s = append(s, i*i)
    	}
    	fmt.Println(s, len(s))
    	fmt.Println(s[1:3])
    	for i, v := range s {
    		fmt.Println(i, v)
    	}
    }
    

    Read the program. What will it print?

    Run it in the playground

  4. 4Read

    Maps

    A map holds key and value pairs. Write m := map[string]int{"a": 1} or make(map[string]int). Read with m["a"], write with m["a"] = 2 and remove with delete(m, "a"). A missing key gives the zero value of the value type. To tell a missing key from a stored zero, use v, ok := m["a"].

  5. 5Code

    Count words with a map

    1 / 2
    package main
    
    import "fmt"
    
    func main() {
    	words := []string{"go", "is", "fun", "go", "go", "is"}
    	counts := make(map[string]int)
    	for _, w := range words {
    		counts[w]++
    	}
    	fmt.Println(counts["go"], counts["is"], counts["fun"])
    	fmt.Println(counts["rust"])
    	n, ok := counts["rust"]
    	fmt.Println(n, ok)
    	delete(counts, "fun")
    	fmt.Println(len(counts))
    }
    

    Read the program. What will it print?

    Run it in the playground

  6. 6Fix

    package main
    
    import "fmt"
    
    func main() {
    	var m map[string]int
    	m["a"] = 1
    	fmt.Println(m["a"])
    }
    
    This program panics. What is the bug?
  7. 7Say it back

  8. 8Mission · 10 min

    Make a slice of five names, append a sixth, print its length, then count how often each first letter appears with a map and print the counts for two letters you pick.

    go run .

Common questions

What is a slice in Go?
A slice is a descriptor of a segment of an array: it holds a pointer to the array, a length and a capacity. Slices are the usual way to keep a list in Go. Copying a slice variable copies only the descriptor, so both variables see the same elements. That is why a change through one slice can show up in another.
How do I add an item to a slice in Go?
Call append and assign the result back: s = append(s, x). The function appends the element to the end of the slice and grows the underlying array when it needs more room. Because append returns the slice it built, ignoring the result loses the new element. You can add several values at once by listing them after the slice.
How do I check if a key exists in a Go map?
Use the two-value form: v, ok := m[key]. The variable ok is true when the key is in the map and false when it is not, in which case v is the zero value of the value type. This tells a missing key apart from a stored zero, which a plain m[key] lookup cannot do.

Sources

Measured with go1.27.4 darwin/arm64 on 2026-12-03

Published Dec 3, 2026 · Updated Dec 3, 2026

Never miss new offers and news

New tools, credits, and AI news are added regularly. Pick how you want to hear about them.

Telegram channel

Join @aifree_dev and get new free AI offers and news right in your Telegram feed.

Browser notifications

Turn on push notifications and get an alert on this device whenever something new is posted. No account needed.

Email newsletter

A weekly digest of new, changed and expiring free AI offers. Double opt-in, one-click unsubscribe. See the latest issue.

Unsubscribe anytime. Privacy

Frequently asked questions

What is aifree.dev?

aifree.dev helps everyone access AI through a curated directory of free AI tools, API credits, free tiers, and startup programs for coding, image, voice, video, and students - verified regularly.

How are offers presented?

Each offer includes its access details, free models when available, and expiration information when provided. Limited-time deals stay visible so they are easy to compare.

Do education and startup programs need verification?

Yes. Providers usually ask students to verify their status with a school email, student ID, or a program like the GitHub Student Pack, and ask startups to verify company details such as age, funding, or website. Check each offer’s claim page for the exact requirements.

What kinds of free AI offers are listed?

Free tiers and free trials from AI providers, free API credits, startup and student programs, and free tools for coding, image generation, voice, and video.

How can I suggest a tool or report a broken link?

Use the submit form: paste the offer page URL and a short note. A person checks every submission before anything is listed. Broken links and changed terms go through the same form.

Know a good one?

Tell us about an AI tool worth knowing. Found a broken link? We want to hear that too.

Submit an offer