6. Confusing Arrays with Slices

6. Confusing Arrays with Slices


Arrays and slices behave differently, and confusing the two can cause problems.

Example:

Modifying the slice also modifies the underlying array, which may not be expected.


Differences between Arrays and Slices

  1. Arrays:
  2. Slices:

Modifying Slices Affects the Underlying Array

When modifying a slice, the underlying array is also affected because both share the same data storage. This can lead to unexpected behaviors if misunderstood.

Example:


Output:


CopyEdit

Array before: [1 2 3] Array after: [10 2 3]

Explanation

  • slice[0] = 10 affects not only the slice but also the array arr, as both share the same underlying data.
  • Being aware of this relationship is crucial to prevent unintended changes.

How to Avoid Issues?

  1. If you need an independent copy, use the copy function:

Now, modifying newSlice won't affect the original array.

2. Always review the scope of changes made to a slice to understand where they will have an impact.


This distinction between arrays and slices is powerful but requires caution to avoid surprises!

Erick Zanetti

Fullstack Engineer | Software Developer | React | Next.js | TypeScript | Node.js | JavaScript | AWS

1 个月

Insightful

回复
Ezequiel Cardoso

.NET Software Engineer | Full Stack Developer | C# | Angular & Blazor | Azure & AWS | Microservices Expert

1 个月

Nice content! Thanks for sharing

回复
Fabio Dallazen

Senior Software Engineer | Ruby On Rails | Backend Developer | AWS | Heroku

1 个月

Great content!

Kleber Augusto dos Santos

AI Solutions Architecture | LLM ML Engineer | Golang | Kotlin | Flutter | React Native | Angular | Figma | Java | .Net | Nodejs | DevOps | Maven | JUnit | CI/CD | GitHub | Design Patterns | Multicloud

1 个月

I agree

Kleber Augusto dos Santos

AI Solutions Architecture | LLM ML Engineer | Golang | Kotlin | Flutter | React Native | Angular | Figma | Java | .Net | Nodejs | DevOps | Maven | JUnit | CI/CD | GitHub | Design Patterns | Multicloud

1 个月

Very helpful

要查看或添加评论,请登录

Ricardo Maia的更多文章

  • 9. Not Using Context (context.Context)

    9. Not Using Context (context.Context)

    When making network calls or running long operations, it's easy to forget to use for timeout or cancellation control…

    25 条评论
  • 8. Poor Variable Naming: Why It Harms Readability

    8. Poor Variable Naming: Why It Harms Readability

    ### 8. Poor Variable Naming: Why It Harms Readability In Go, simplicity and clarity are core principles.

    64 条评论
  • ?? Common Go Mistake: Overusing init() — Keep It Simple! ??

    ?? Common Go Mistake: Overusing init() — Keep It Simple! ??

    Ever seen a Go package where is doing heavy lifting—like reading configs, connecting to databases, or launching…

    22 条评论
  • 5. Ignoring Race Conditions

    5. Ignoring Race Conditions

    Failing to protect shared variables can result in race conditions. Example: Best Practice: Use or other synchronization…

    36 条评论
  • 4. Inefficient Use of Pointers

    4. Inefficient Use of Pointers

    Handling pointers in Go is powerful but can introduce bugs. Example: The first code snippet attempts to assign a value…

    53 条评论
  • 3. Failing to Synchronize Goroutines

    3. Failing to Synchronize Goroutines

    Forgetting to use or other synchronization mechanisms can lead to unexpected behavior. Example: Best Practice: Use to…

    39 条评论
  • 2. Misusing Goroutines

    2. Misusing Goroutines

    Improper use of goroutines can lead to leaks or deadlocks. A classic example is forgetting to close channels.

    63 条评论
  • 1. Ignoring Returned Errors

    1. Ignoring Returned Errors

    Go emphasizes explicit error handling, but it's common to overlook this, especially in quick tests. Example: Best…

    30 条评论
  • Working with Cobra CLI in Go: A Guide to Building Robust Command-Line Tools

    Working with Cobra CLI in Go: A Guide to Building Robust Command-Line Tools

    Creating command-line tools with Cobra CLI in Go is an excellent choice for developing robust and scalable…

    19 条评论
  • How Middlewares Work in Next.js

    How Middlewares Work in Next.js

    Middlewares in Next.js are functions executed between the user's request and the server's response.

    31 条评论

社区洞察