mirror of
https://github.com/teivah/100-go-mistakes.git
synced 2026-06-20 16:45:56 +08:00
38 lines
581 B
Go
38 lines
581 B
Go
package main
|
|
|
|
func convertEmptySlice(foos []Foo) []Bar {
|
|
bars := make([]Bar, 0)
|
|
|
|
for _, foo := range foos {
|
|
bars = append(bars, fooToBar(foo))
|
|
}
|
|
return bars
|
|
}
|
|
|
|
func convertGivenCapacity(foos []Foo) []Bar {
|
|
n := len(foos)
|
|
bars := make([]Bar, 0, n)
|
|
|
|
for _, foo := range foos {
|
|
bars = append(bars, fooToBar(foo))
|
|
}
|
|
return bars
|
|
}
|
|
|
|
func convertGivenLength(foos []Foo) []Bar {
|
|
n := len(foos)
|
|
bars := make([]Bar, n)
|
|
|
|
for i, foo := range foos {
|
|
bars[i] = fooToBar(foo)
|
|
}
|
|
return bars
|
|
}
|
|
|
|
type Foo struct{}
|
|
|
|
type Bar struct{}
|
|
|
|
func fooToBar(foo Foo) Bar {
|
|
return Bar{}
|
|
}
|