Skip to main content

Command Palette

Search for a command to run...

Go Slice: Length vs Capacity

Updated
3 min read
Go Slice: Length vs Capacity
I

A highly motivated and experienced full-stack developer with a proven track record of developing and deploying web applications. Skilled in a range of programming languages and frameworks, as well as database technologies. Comfortable working in a fast-paced environment and able to adapt to new technologies quickly. A team player who is also able to work independently when required.

Go তে slice হচ্ছে একটি super powerful data structure। কিন্তু অনেক এর কাছেই slice এর length আর capacity নিয়ে confusion থাকে। আজকে আমরা এই দুটো জিনিস নিয়ে আলোচনা করব।

আসল সমস্যাটা কী?

এই code টা দেখুন:

package main

import "fmt"

func main() {
    s := make([]int, 3, 10)
    s[0] = 5
    s[1] = 6
    s[2] = 7
    s[3] = 8  //  Runtime error: index out of range!

    fmt.Println(s)
}

অনেক developer অবাক হন যখন এই code runtime এ error দেয়। আমরা capacity 10 দিয়ে slice বানিয়েছি, তাহলে index 3 (চতুর্থ element) access করতে পারছি না কেন?

Length vs Capacity: আসল পার্থক্য

Go তে slice এর দুটো important property আছে:

  • Length: আপনি এখন যতগুলো element access করতে পারবেন

  • Capacity: underlying array তে maximum যতগুলো element রাখা যাবে

যখন আপনি make([]int, 3, 10) লিখেন, তখন আপনি আসলে বলছেন:

  • আমি এখন 3টা element access করতে চাই (length)

  • আমি future এর জন্য 10টা element রাখার জায়গা reserve করতে চাই (capacity)

Go কেন এভাবে design করা হয়েছে

Go language safety আর clarity কে priority দেয়। Length এর limit enforce করে Go অনেক common programming mistake prevent করে:

  1. Uninitialized memory access

  2. Garbage value নিয়ে কাজ করা

  3. Buffer overflow যা security risk create করতে পারে

Extra Capacity কীভাবে ব্যবহার করবেন

Capacity আছে future growth এর জন্য, সরাসরি access করার জন্য নয়। এটা use করার দুটো main উপায় আছে:

Option 1: append() ব্যবহার করা

s := make([]int, 3, 10)
s[0] = 5
s[1] = 6
s[2] = 7
s = append(s, 8)  // Safely নতুন element যোগ করে length বাড়ায়

fmt.Println(s)    // [5 6 7 8]

Option 2: Re-slice করে length বাড়ানো

s := make([]int, 3, 10)
s[0] = 5
s[1] = 6
s[2] = 7

s = s[:4]         // Slice এর visible অংশ বাড়িয়ে দিলাম
s[3] = 8          // এখন index 3 access করা safe

fmt.Println(s)    // [5 6 7 8]

একটা Visual ব্যাখ্যা

Slice কে ভাবুন array এর উপর একটা window হিসেবে:

  • Length: আপনার window এখন কতটা বড়

  • Capacity: আপনার window maximum কতটা বড় হতে পারে

  • Re-slicing: Window এর size adjust করা

  • Appending: Item যোগ করে আপনার window আরো বড় করা

Performance Benefits

এই design এ অনেক performance benefit পাওয়া যায়:

  1. Memory Allocation কমে: যখন sufficient capacity থাকে, append করলে Go কে new underlying array create করতে হয় না।

  2. Predictable Growth: আপনি আগে থেকেই expected size অনুযায়ী capacity set করতে পারেন, যাতে expensive reallocation এড়ানো যায়।

Best Practices

  1. Capacity সম্পর্কে Clear থাকুন:

     // যদি আপনি জানেন আপনার প্রায় 1000 element লাগবে
     data := make([]int, 0, 1000)
    
  2. Growth Pattern বিবেচনা করুন:

     // ধীরে ধীরে বাড়ার জন্য
     data := make([]int, 0, 100)  // শুরুতে length 0, কিন্তু বাড়ার জায়গা আছে
    
  3. Initialization এর জন্য Length ব্যবহার করুন:

     // যদি শুরুতে zeros দিয়ে array দরকার হয়
     data := make([]int, 10)  // Length 10, সব 0 দিয়ে initialized
    
  4. Bounds Check করুন:

     if i < len(s) {
         s[i] = value  // Safe access
     }
    

Conclusion

Length আর capacity এর মধ্যে পার্থক্য বোঝা Go তে efficient programming এর জন্য খুব important। এই rules follow করলে আপনি safe, predictable code লিখতে পারবেন এবং slice এর performance benefits পাবেন।

পরের বার যখন extra capacity দিয়ে slice বানাবেন, মনে রাখবেন: ওই extra space future growth (append বা re-slicing) এর জন্য—direct indexing এর জন্য নয়।

Happy coding!


10 views

More from this blog

Low Level Design: গভীর থেকে বোঝা এবং আয়ত্ত করা

ভূমিকা: কেন এই Article? তুমি হয়তো programming শিখেছ। Variable, loop, function, data structure - সব জানো। কিন্তু যখন একটা বড় system বানাতে বসো, তখন মনে হয় কোথা থেকে শুরু করব? কীভাবে organize করব? Code লিখতে লিখতে হারিয়ে যাও একটা maze-এ। এই feeling...

Oct 15, 202520 min read71
Low Level Design: গভীর থেকে বোঝা এবং আয়ত্ত করা

Go-তে Interface কীভাবে Code Decouple করে?

একটা HTTP Server দিয়ে পুরো ব্যাপারটা বুঝে নেওয়া যাক আমরা সবাই জানি Go একটা সিম্পল ল্যাঙ্গুয়েজ, কিন্তু interface নিয়ে অনেকেরই confusion থাকে। আজকে আমরা দেখব কীভাবে interface আসলে তোমার code-কে flexible এবং maintainable বানায়। একটা real-world HTT...

Oct 14, 202520 min read5
Go-তে Interface কীভাবে Code Decouple করে?

তোমার Project-এ Coupled Code কীভাবে খুঁজে বের করবে?

Coupled code খোঁজা মানে হচ্ছে তোমার codebase-এ এমন জায়গা খুঁজে বের করা যেখানে একটা অংশ আরেকটার উপর বেশি depend করছে। এটা একটা detective work — তুমি clue খুঁজবে, pattern দেখবে, এবং সমস্যা চিহ্নিত করবে। চলো step by step শিখি কীভাবে এটা করতে হয়। কেন ...

Oct 14, 202510 min read2

Go-তে Object (Struct Instance) তৈরির সম্পূর্ণ গাইড

Go programming শেখার সময় একটা জিনিস খুব তাড়াতাড়ি বুঝতে হয় - কীভাবে object তৈরি করতে হয়। অন্য language যেমন Java বা Python এ class আছে, কিন্তু Go-তে আছে struct। আর struct এর instance বানানোই হলো object তৈরি করা। আজকের এই blog এ আমরা দেখব Go-তে ob...

Oct 13, 202524 min read3
Go-তে Object (Struct Instance) তৈরির সম্পূর্ণ গাইড
I

Imran Hasan

61 posts

Full-stack developer with experience in developing and managing web applications. Skilled in React, Node.js, HTML, CSS, and JavaScript. Experience in managing website hosting and security.