go-data-structureslisted
Install: claude install-skill dwana1/golang-skills
# Go Data Structures
> **Source**: Effective Go
This skill covers Go's built-in data structures and allocation primitives.
---
## Allocation: new vs make
Go has two allocation primitives: `new` and `make`. They do different things.
### new
`new(T)` allocates zeroed storage for a new item of type `T` and returns `*T`:
```go
p := new(SyncedBuffer) // type *SyncedBuffer, zeroed
var v SyncedBuffer // type SyncedBuffer, zeroed
```
**Zero-value design**: Design data structures so the zero value is useful without
further initialization. Examples: `bytes.Buffer`, `sync.Mutex`.
```go
type SyncedBuffer struct {
lock sync.Mutex
buffer bytes.Buffer
}
// Ready to use immediately upon allocation
```
### make
`make(T, args)` creates slices, maps, and channels only. It returns an
**initialized** (not zeroed) value of type `T` (not `*T`):
```go
make([]int, 10, 100) // slice: length 10, capacity 100
make(map[string]int) // map: ready to use
make(chan int) // channel: ready to use
```
### The Difference
```go
var p *[]int = new([]int) // *p == nil; rarely useful
var v []int = make([]int, 100) // v is a usable slice of 100 ints
// Idiomatic:
v := make([]int, 100)
```
**Rule**: `make` applies only to maps, slices, and channels and does not return
a pointer.
---
## Composite Literals
Create and initialize structs, arrays, slices, and maps in one expression:
```go
// Struct with positional fields
f := File{fd, name, nil, 0}
// Struct with nam