← Back to Go Course | Chapter 10: Packages & Modules | Lesson 2 of 6

Packages Import करना

किसी package को import करना किसी और के toolbox से tools का एक specific set उधार लेकर उन्हें अपनी workspace में लाने जैसा है ताकि आप उन्हें उपयोग कर सकें।
Syntax
go
import "package/path"

import (
    "package/one"
    alias "package/two"
    _ "package/three"
)

Basic Import Syntax

import keyword, quotes में किसी package के import path के बाद, उस package के exported identifiers को उपलब्ध बनाता है, package के नाम (आमतौर पर इसके path का आखिरी element) के बाद एक dot से access किया गया।

उदाहरण: Basic Import Syntax

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() {
	// Print the values followed by a newline
	fmt.Println("fmt package imported and used")
}

Grouped Imports

कई packages import करते समय, Go convention हर line पर import keyword दोहराने के बजाय उन्हें एक parenthesized block में group करता है, जिसे gofmt साफ़ sorted रखता है।

Note: grouped imports को अपने आप alphabetically sorted रखने के लिए 'go fmt' चलाएं।

उदाहरण: Grouped Imports

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"strings"
)

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println(strings.ToUpper("go imports"))
}

Aliasing an Import

किसी import को इसके path से पहले एक identifier रखकर एक अलग local name दिया जा सकता है, तब उपयोगी जब दो imported packages अन्यथा एक जैसा default name share करतीं।

उदाहरण: Aliasing an Import

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	str "strings"
)

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println(str.ToLower("ALIASED IMPORT"))
}

Blank Imports for Side Effects

किसी import path को _ से prefix करना इसे बिल्कुल इसके side effects के लिए import करता है (जैसे कुछ register करता एक init() function), इसके identifiers को उपयोग करने योग्य बनाए बिना या 'unused import' error trigger किए बिना।

उदाहरण: Blank Imports for Side Effects

markup
package main

import (
	"fmt"
	_ "strings" // blank import: runs init side effects only, not actually needed here
)

func main() {
	fmt.Println("blank imports run side effects without exposing identifiers")
}
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. किसी file में एक unused import छोड़ना, जो Go में सिर्फ एक lint warning नहीं बल्कि एक compile-time error है।
  2. imported identifiers को package name से prefix करना भूल जाना, जैसे fmt.Println() के बजाय Println() call करना।
  3. किसी package को बिल्कुल इसके side effects के लिए blank identifier (_) prefix उपयोग किए बिना import करना, एक 'imported and not used' error का कारण बनते हुए।
चैप्टर सारांश
  • import "path" किसी package को scope में लाता है, इसके package name को prefix के रूप में access किया गया।
  • Convention के अनुसार कई imports एक single parenthesized import block में group किए जाते हैं।
  • एक unused import एक compile-time error है, import lists को हमेशा accurate रखते हुए।
  • एक blank import, _ "path", किसी package के init() side effects को इसके identifiers उजागर किए बिना चलाता है।
🔒

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.