मूल Data Types
In this page:
var name int // int, int8..int64, uint, uintptr
var name float64 // float32, float64
var name bool
var name string
Integers
Go एक general-purpose int (जिसका size platform से मेल खाता है, 32 या 64 bit) और int8, int16, int32, और int64 जैसे explicitly sized integer types, साथ ही कभी negative न होने वाली values के लिए unsigned versions (uint8, uint32, आदि) दोनों प्रदान करता है।
उदाहरण: Integers
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Declare int and set it to 42
var count int = 42
// Declare int8 and set it to 120
var small int8 = 120
var big int64 = 9000000000
// Print the values followed by a newline
fmt.Println(count, small, big)
}
Login to try C/C++/Java/PHP code in the editor
Floating-Point Numbers
float32 और float64 decimal point वाली numbers represent करते हैं। float64 default है और आमतौर पर precision के लिए पसंद किया जाता है जब तक memory tightly constrained न हो, क्योंकि float32 बहुत बड़ी या बहुत precise values के लिए accuracy खो देता है।
उदाहरण: Floating-Point Numbers
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Declare float64 and set it to 19.99
var price float64 = 19.99
// Declare float32 and set it to 0.15
var discount float32 = 0.15
// Print the values followed by a newline
fmt.Println("price:", price, "discount:", discount)
}
Login to try C/C++/Java/PHP code in the editor
Booleans
bool type बिल्कुल दो values में से एक रखता है, true या false, और यह हर comparison operator (==, <, >, आदि) का result है। Booleans if statements और loop conditions में branching logic control करते हैं।
उदाहरण: Booleans
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create isActive and infer the type
isActive := true
// Short declaration: create hasExpired and infer the type
hasExpired := false
// Print the values followed by a newline
fmt.Println("active:", isActive, "expired:", hasExpired)
}
Login to try C/C++/Java/PHP code in the editor
Strings
Go में एक string bytes का एक immutable sequence है, सबसे आम तौर पर UTF-8 encoded text रखता है। String literals double quotes में लिखे जाते हैं, और Go उनके साथ काम करने के लिए कई built-in operators और standard-library functions प्रदान करता है।
उदाहरण: Strings
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create greeting and infer the type
greeting := "Namaste"
// Print the values followed by a newline
fmt.Println(greeting, "length in bytes:", len(greeting))
}
Login to try C/C++/Java/PHP code in the editor
- हर जगह int उपयोग करना और यह मान लेना कि यह हमेशा 64-bit है -- इसका size असल में target platform (32 या 64 bit) पर निर्भर करता है।
- बिना explicit conversion के एक ही expression में float32 और float64 values मिलाना, जिसे Go का type system reject करता है।
- equality के लिए floating-point numbers को == से compare करना जब rounding error दो 'बराबर' values को थोड़ा अलग बना सकता है।
- Go के core basic types में int, float64, bool, और string शामिल हैं।
- Integer types int8, int16, int32, int64 और उनके unsigned counterparts जैसे sized variants में आते हैं।
- float32 और float64 decimal numbers represent करते हैं, float64 आम default होने के साथ।
- Go सख्त है: यह बिना explicit conversion के किसी expression में कभी types को चुपचाप नहीं मिलाता।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: