← Back to Go Course | Chapter 12: Generics | Lesson 1 of 6

Generics परिचय

Generics आपको एक recipe लिखने देते हैं जो कई अलग तरह के ingredients के लिए काम करे, बजाय हर ingredient type के लिए लगभग एक जैसी recipe लिखने के।
Syntax
go
func FunctionName[T any](param T) T {
	return param
}

FunctionName[int](value)   // explicit type
FunctionName(value)        // type inferred

Why Generics?

Generics से पहले, एक function लिखना जो []int और []string दोनों पर काम करे का मतलब था या तो function को हर type के लिए दोहराना या interface{} पर वापस जाना और compile-time type safety खोना।

Generics इसे हल करते हैं एक function definition को कई types में काम करने देकर जबकि compiler अब भी types जांचता है।

उदाहरण: Why Generics?

markup
package main

import "fmt"

func PrintAll[T any](items []T) {
	for _, item := range items {
		fmt.Println(item)
	}
}

func main() {
	PrintAll([]int{1, 2, 3})
	PrintAll([]string{"a", "b"})
}

A Simple Generic Function

एक generic function अपने नाम के ठीक बाद square brackets में एक या ज़्यादा type parameters declare करता है, जिन्हें फिर function के signature और body में कहीं भी किसी दूसरे type की तरह उपयोग किया जा सकता है।

उदाहरण: A Simple Generic Function

markup
package main

import "fmt"

func Max[T int | float64](a, b T) T {
	if a > b {
		return a
	}
	return b
}

func main() {
	fmt.Println(Max(3, 7))
	fmt.Println(Max(2.5, 1.1))
}

Generics Are Still Type-Checked

भले ही generic code कई types के लिए एक बार लिखा जाता है, Go अब भी compile time पर हर instantiation को पूरी तरह type-check करता है -- किसी generic function को एक ऐसी type के साथ call करना जो इसके constraints satisfy नहीं करती एक compile error है, कोई runtime surprise नहीं।

उदाहरण: Generics Are Still Type-Checked

markup
package main

import "fmt"

func First[T any](items []T) T {
	return items[0]
}

func main() {
	fmt.Println(First([]string{"go", "generics"}))
	fmt.Println(First([]bool{true, false}))
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि Go में शुरू से generics थे -- इन्हें सिर्फ Go 1.18 (2022) में जोड़ा गया, इसलिए पुराने code और tutorials अक्सर इन्हें पूरी तरह टालते हैं।
  2. हर function के लिए generics का सहारा लेना, भले ही एक साधारण interface या कुछ concrete-typed functions ज़्यादा सरल होते।
  3. यह भूल जाना कि type parameter को regular parameter list से पहले square brackets में declare होना चाहिए।
चैप्टर सारांश
  • Go 1.18 में जोड़े गए Generics, functions और types को कई types के साथ काम करने देते हैं जबकि type-safe रहते हुए।
  • एक type parameter regular parameters से पहले square brackets में declared होता है, जैसे func Max[T int | float64](a, b T) T।
  • Generics हर concrete type के लिए लगभग-एक-जैसा code दोहराने से बचाते हैं।
  • Go अब भी generic code को compile time पर resolve करता है -- कोई runtime type-erasure surprise नहीं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.