← Back to Go Course | Chapter 2: Variables & Types | Lesson 4 of 7

मूल Data Types

Go के basic types अलग-अलग तरह के boxes हैं जिनमें से आप चुन सकते हैं: whole numbers, decimals, true/false, और text।
Syntax
go
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, आदि) दोनों प्रदान करता है।

Note: साधारण int उपयोग करें जब तक आपके पास कोई specific वजह (जैसे memory layout या binary formats) न हो एक sized variant चुनने की।

उदाहरण: Integers

markup
// 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)
}

Floating-Point Numbers

float32 और float64 decimal point वाली numbers represent करते हैं। float64 default है और आमतौर पर precision के लिए पसंद किया जाता है जब तक memory tightly constrained न हो, क्योंकि float32 बहुत बड़ी या बहुत precise values के लिए accuracy खो देता है।

उदाहरण: Floating-Point Numbers

markup
// 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)
}

Booleans

bool type बिल्कुल दो values में से एक रखता है, true या false, और यह हर comparison operator (==, <, >, आदि) का result है। Booleans if statements और loop conditions में branching logic control करते हैं।

उदाहरण: Booleans

markup
// 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)
}

Strings

Go में एक string bytes का एक immutable sequence है, सबसे आम तौर पर UTF-8 encoded text रखता है। String literals double quotes में लिखे जाते हैं, और Go उनके साथ काम करने के लिए कई built-in operators और standard-library functions प्रदान करता है।

उदाहरण: Strings

markup
// 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))
}
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. हर जगह int उपयोग करना और यह मान लेना कि यह हमेशा 64-bit है -- इसका size असल में target platform (32 या 64 bit) पर निर्भर करता है।
  2. बिना explicit conversion के एक ही expression में float32 और float64 values मिलाना, जिसे Go का type system reject करता है।
  3. 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:

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.