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

अपने खुद के Packages बनाना

अपना खुद का package बनाना अपनी खुद की labeled toolbox drawer शुरू करने जैसा है जिससे आपके project (या दूसरे लोगों के projects) के बाकी हिस्से tools उधार ले सकें।
Syntax
go
// myapp/util/util.go
package util

func Exported() {}

// main.go
import "myapp/util"
util.Exported()

Creating a Package Directory

एक reusable package बनाने के लिए, एक नई subdirectory बनाएं और इसके अंदर की हर .go file को एक जैसी package declaration दें, directory के मकसद से मेल खाती -- उदाहरण के लिए 'package mathutils' रखने वाला एक mathutils folder।

उदाहरण: Creating a Package Directory

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("myapp/mathutils/add.go declares: package mathutils")
}

Exporting Identifiers

किसी package में सिर्फ capitalized function, type, और variable नाम इसे import करने वाले code को दिखाई देते हैं -- lowercase identifiers private implementation details बने रहते हैं, package के बाहर अदृश्य।

उदाहरण: Exporting Identifiers

markup
package main

import "fmt"

// Simulating what would live in a separate mathutils package file:
// func Add(a, b int) int { return a + b }       -- exported, usable elsewhere
// func subtractInternal(a, b int) int { ... }    -- unexported, package-private

func Add(a, b int) int {
	return a + b
}

func main() {
	fmt.Println(Add(3, 4))
}

Importing Your Own Package

आपकी लिखी एक package उसी तरह import होती है जैसे किसी third-party वाली, module के नाम (go.mod में) plus इसकी subdirectory से बने इसके पूरे path का उपयोग करते हुए, जैसे github.com/example/myapp/mathutils।

Note: Import path हमेशा go.mod की पहली line में declared module path के सापेक्ष होता है।

उदाहरण: Importing Your Own Package

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("import \"github.com/example/myapp/mathutils\"")
	// Print the values followed by a newline
	fmt.Println("then call: mathutils.Add(3, 4)")
}
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. किसी ऐसे identifier को capitalize करना भूल जाना जो दूसरे packages से उपयोग होने योग्य होना चाहिए था, इसे अनजाने में private छोड़ते हुए।
  2. util या common जैसे अत्यधिक generic नाम वाला package बनाना जो असंबंधित code के लिए एक dumping ground बन जाता है।
  3. module root के सापेक्ष गलत directory में package code रखना, इसके expected import path को तोड़ते हुए।
चैप्टर सारांश
  • एक custom package main के अलावा बस एक package name share करती .go files की एक directory है।
  • Package को go.mod में declared इसके module root के सापेक्ष इसके path का उपयोग करके import किया जाता है।
  • Exported identifiers (capitalized) दूसरे code को उपयोग करने के लिए package का public API बन जाते हैं।
  • अच्छी तरह designed packages की एक छोटी, केंद्रित responsibility होती है बजाय catch-all होने के।
🔒

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.