What is Type Inference in Go?
Type Inference एक ऐसी feature है जो Go compiler को बिना explicitly type declare किए हुए, variables के types को automatically determine करने की अनुमति देती है। इसका मतलब है कि आप variable को initialize करते समय उसके type को explicitly लिखने की बजाय, Go compiler उस variable के type को infer (अनुमान) कर लेता है, जो कि assigned value के आधार पर होता है।
Basic Syntax for Type Inference
Go में Type Inference का उपयोग करने के लिए आप shorthand notation (:=) का उपयोग कर सकते हैं। यह syntax variable को declare और initialize करता है, और Go compiler automatically उसके type को infer कर लेता है।
Example: Basic Type Inference
package main
import "fmt"
func main() {
// Type inference: Compiler will infer the type of 'x' as 'int'
x := 10
fmt.Printf("Type of x: %T\n", x)
// Type inference: Compiler will infer the type of 'y' as 'float64'
y := 10.5
fmt.Printf("Type of y: %T\n", y)
// Type inference: Compiler will infer the type of 'z' as 'string'
z := "Hello, Go!"
fmt.Printf("Type of z: %T\n", z)
}
Explanation:
x := 10:यहांxकी typeintinferred हो जाती है क्योंकि उसे integer value10assign की गई है।y := 10.5:यहांyकी typefloat64inferred हो जाती है क्योंकि उसे floating-point value10.5assign की गई है।z := "Hello, Go!":यहांzकी typestringinferred हो जाती है क्योंकि उसे string value"Hello, Go!"assign की गई है।%T:यह format specifier है, जो variable की type को print करता है।
1. Type Inference with Functions
जब आप किसी function की return value को directly किसी variable में assign करते हैं, तो Go compiler उस variable की type को automatically infer कर लेता है।
Example: Type Inference with Function Return Values
package main
import "fmt"
func add(a, b int) int {
return a + b
}
func main() {
// Type inference: The type of 'result' is inferred as 'int'
result := add(3, 4)
fmt.Printf("Type of result: %T\n", result)
}
Explanation:
result := add(3, 4):यहां functionaddकी return value की typeintहोती है, और Go compiler automatically variableresultकी type कोintinfer कर लेता है।
2. Type Inference with Constants
Constants के साथ भी type inference apply होता है। जब आप किसी constant को किसी variable में assign करते हैं, तो Go compiler उस variable की type को constant की value के आधार पर infer कर लेता है।
Example: Type Inference with Constants
package main
import "fmt"
const Pi = 3.14
func main() {
// Type inference: The type of 'radius' is inferred as 'float64'
radius := Pi
fmt.Printf("Type of radius: %T\n", radius)
}
Explanation:
radius := Pi:यहांPiकी typefloat64है, और Go compiler automaticallyradiusकी type कोfloat64infer कर लेता है।
3. Type Inference with Composite Literals
Composite literals जैसे slices, maps, और structs के साथ भी type inference apply होता है। जब आप एक composite literal को initialize करते हैं, तो Go compiler उसकी type को automatically infer कर लेता है।
Example: Type Inference with Slices
package main
import "fmt"
func main() {
// Type inference: The type of 'numbers' is inferred as '[]int' (slice of integers)
numbers := []int{1, 2, 3, 4, 5}
fmt.Printf("Type of numbers: %T\n", numbers)
}
Explanation:
numbers := []int{1, 2, 3, 4, 5}:यहां Go compiler automaticallynumbersकी type को[]int(integer slice) infer कर लेता है।
Example: Type Inference with Maps
package main
import "fmt"
func main() {
// Type inference: The type of 'ages' is inferred as 'map[string]int'
ages := map[string]int{
"Alice": 30,
"Bob": 25,
}
fmt.Printf("Type of ages: %T\n", ages)
}
Explanation:
ages := map[string]int{...}:यहां Go compiler automaticallyagesकी type कोmap[string]intinfer कर लेता है।
4. Type Inference with Interfaces
जब आप किसी value को interface type के variable में assign करते हैं, तो Go compiler उस value की underlying type को infer कर लेता है। हालांकि, interface type variable की type हमेशा interface ही रहेगी।
Example: Type Inference with Interfaces
package main
import "fmt"
func main() {
var i interface{}
// Type inference: The type of 'i' is inferred as 'int'
i = 42
fmt.Printf("Value of i: %v, Type of i: %T\n", i, i)
// Type inference: The type of 'i' is inferred as 'string'
i = "Hello"
fmt.Printf("Value of i: %v, Type of i: %T\n", i, i)
}
Explanation:
i = 42:यहांiको integer value assign की गई है, और underlying typeintinferred हो जाती है।i = "Hello":यहांiको string value assign की गई है, और underlying typestringinferred हो जाती है।
5. Type Inference Limitations
हालांकि Go में type inference powerful है, लेकिन कुछ limitations भी हैं:
- आप type inference का उपयोग केवल उन cases में कर सकते हैं जहां variable को initialize किया जा रहा हो। Uninitialized variables के लिए आपको explicit type declaration की आवश्यकता होती है।
- Type inference केवल compile-time पर होता है, इसलिए runtime पर variable की type change नहीं की जा सकती।
Summary
- Type Inference: Go compiler automatically variable की type को infer करता है, जब आप उसे initialize करते हैं।
- Shorthand Notation (:=): Type inference के लिए shorthand notation का उपयोग किया जाता है, जो variable को declare और initialize करता है।
- Function Return Values: Functions से returned values की type को भी automatically infer किया जा सकता है।
- Composite Literals: Slices, maps, और structs के साथ भी type inference apply होता है।
- Interfaces: Interface variables में underlying type को infer किया जाता है, लेकिन interface type itself नहीं बदलती।
Type inference से code को concise और readable बनाया जा सकता है, लेकिन आपको यह ध्यान रखना चाहिए कि Go में यह feature strict typing के साथ आता है। अगर आपको type inference के बारे में और जानकारी चाहिए या कोई specific सवाल है, तो बताइए!
No comments:
Post a Comment