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

go.mod और go.sum की जानकारी

go.mod आपके project की ज़रूरी ingredients की shopping list है, और go.sum वह receipt है जो साबित करती है कि हर ingredient बिल्कुल वही है जो यह claim करती है।
Syntax
go
module module/path

go 1.21

require example.com/pkg v1.2.3

// go.sum: module version hash

What go.mod Contains

go.mod module का अपना import path, यह target करने वाला minimum Go version, और हर direct (और कभी-कभी indirect) dependency को exact version के साथ list करने वाला एक require block record करता है।

उदाहरण: What go.mod Contains

bash
module github.com/example/myapp

go 1.22

require github.com/some/dependency v1.4.0

⚠️ Run this in your own terminal or Node.js environment.

What go.sum Contains

go.sum हर dependency की exact content के लिए cryptographic hashes store करता है, ताकि project बनाने वाले किसी को भी bit-for-bit एक जैसा code मिले -- किसी compromised या गलती से बदले गए package से सुरक्षा देते हुए।

उदाहरण: What go.sum Contains

bash
github.com/some/dependency v1.4.0 h1:abcdef1234567890...
github.com/some/dependency v1.4.0/go.mod h1:1234567890abcdef...

⚠️ Run this in your own terminal or Node.js environment.

Keeping Dependencies Tidy

'go mod tidy' आपके source code की असली imports scan करता है और go.mod/go.sum को exactly उससे मेल खाने के लिए update करता है -- कुछ भी missing जोड़ते हुए और उन dependencies को हटाते हुए जो अब असल में कहीं import नहीं होतीं।

Note: जब भी आप imports जोड़ें या हटाएं, commit करने से पहले 'go mod tidy' चलाएं।

उदाहरण: Keeping Dependencies Tidy

bash
go mod tidy

⚠️ Run this in your own terminal or Node.js environment.

Why Commit Both Files

go.mod और go.sum को version control में साथ commit करना यह गारंटी देता है कि हर developer, और हर CI build, dependencies को verified content के साथ बिल्कुल एक जैसी versions में resolve करे -- यही Go builds को reproducible बनाता है।

उदाहरण: Why Commit Both Files

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("go.mod + go.sum, committed together, make builds reproducible")
}
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. go.sum को हाथ से edit करना -- यह machine-generated है और इसे सिर्फ go commands चलाकर update किया जाना चाहिए।
  2. यह सोचकर go.sum delete करना कि यह अनावश्यक है, जबकि यह वही है जो build को छेड़छाड़ या corrupted dependencies से बचाता है।
  3. go.mod commit करना लेकिन go.sum commit करना भूल जाना, दूसरे developers के लिए reproducible builds तोड़ते हुए।
चैप्टर सारांश
  • go.mod module की identity, Go version, और इसकी required dependencies को versions के साथ declare करता है।
  • go.sum हर dependency की अखंडता verify करते हुए cryptographic checksums record करता है।
  • दोनों files को हमेशा version control में साथ commit करना चाहिए।
  • 'go mod tidy' go.mod और go.sum को accurate रखता है, missing जोड़ते हुए और unused requirements हटाते हुए।
🔒

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.