Packages Import करना
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
// 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")
}
Login to try C/C++/Java/PHP code in the editor
Grouped Imports
कई packages import करते समय, Go convention हर line पर import keyword दोहराने के बजाय उन्हें एक parenthesized block में group करता है, जिसे gofmt साफ़ sorted रखता है।
उदाहरण: Grouped Imports
// 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"))
}
Login to try C/C++/Java/PHP code in the editor
Aliasing an Import
किसी import को इसके path से पहले एक identifier रखकर एक अलग local name दिया जा सकता है, तब उपयोगी जब दो imported packages अन्यथा एक जैसा default name share करतीं।
उदाहरण: Aliasing an Import
// 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"))
}
Login to try C/C++/Java/PHP code in the editor
Blank Imports for Side Effects
किसी import path को _ से prefix करना इसे बिल्कुल इसके side effects के लिए import करता है (जैसे कुछ register करता एक init() function), इसके identifiers को उपयोग करने योग्य बनाए बिना या 'unused import' error trigger किए बिना।
उदाहरण: Blank Imports for Side Effects
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")
}
Login to try C/C++/Java/PHP code in the editor
- किसी file में एक unused import छोड़ना, जो Go में सिर्फ एक lint warning नहीं बल्कि एक compile-time error है।
- imported identifiers को package name से prefix करना भूल जाना, जैसे fmt.Println() के बजाय Println() call करना।
- किसी 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: