How to clear a slice in Go? All methods and pitfalls
Hello, colleagues! I've just started learning Go and encountered a problem that seems trivial, but I can't find a clear solution. I have a slice that I actively use in a loop to accumulate data. After each iteration, I need to completely clear this slice to start filling it again.
Here's a simplified example of what I'm doing:
```go
package main
import "fmt"
func main() {
data := make([]int, 0, 10) // create a slice with initial capacity 10
for i := 0; i < 3; i++ {
// Fill the slice with data
for j := 0; j < 5; j++ {
data = append(data, j)
}
fmt.Println("Iteration", i, "data:", data)
// And here I need to clear data before the next iteration
// d
```
No answers yet.
Log in to leave an answer
Blogs
Optimizing C++ Application Performance: For Beginners
Profiling Kotlin Code: Finding Bottlenecks for Beginners
Downloading a File on Android with Progress Indicator: A Complete Guide
Ctrl+C in Go: Intercepting SIGINT and "defer" Cleanup Under the Hood