← Back to Go Course | Chapter 1: Setup & Basics | Lesson 6 of 7

Go Project की Workspace Structure

किसी Go project का folder layout एक toolbox organize करने जैसा है -- हर drawer (folder) का एक स्पष्ट काम होता है ताकि कोई भी जो चाहिए वह ढूंढ सके।

A Minimal Layout

सबसे simple संभव Go project बस एक ही directory में एक go.mod file और एक main.go है। जैसे-जैसे project बढ़ता है, code को subdirectories में बांटा जाता है, हर subdirectory अपना import होने योग्य package बनता है जिसका नाम उसके folder पर होता है।

उदाहरण: A Minimal Layout

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/")
	// Print the values followed by a newline
	fmt.Println("  go.mod")
	fmt.Println("  main.go")
}

Organizing Packages by Folder

.go files वाली हर directory (root के अलावा) आमतौर पर एक package represent करती है, जिसे module root के सापेक्ष इसके path से import किया जाता है, जैसे github.com/example/myapp/utils।

यह convention संबंधित code को साथ रखता है और सिर्फ file tree देखकर import graph को follow करना आसान बनाता है।

Note: Package names conventionally छोटे, lowercase होते हैं, और folder name से मेल खाते हैं।

उदाहरण: Organizing Packages by Folder

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 layout and infer the type
	layout := []string{"myapp/go.mod", "myapp/main.go", "myapp/utils/strings.go"}
	// Loop over layout; the blank identifier _ ignores the index
	for _, path := range layout {
		// Print the values followed by a newline
		fmt.Println(path)
	}
}

The internal/ Convention

Go internal नाम की directory के अंदर किसी भी package को खासतौर पर treat करता है: इसे सिर्फ उस internal directory के parent पर rooted code ही import कर सकता है।

यह packages को private implementation details के रूप में चिह्नित करने का standard तरीका है जिन पर बाहरी modules को निर्भर नहीं रहना चाहिए।

उदाहरण: The internal/ Convention

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/internal/auth can only be imported from within myapp")
}

Where Tests Live

Go test files उस code के ठीक बगल में रहती हैं जिन्हें वे cover करती हैं और इनका नाम _test.go suffix से होता है, जैसे strings.go के साथ strings_test.go।

'go test' tool अपने आप किसी package में हर _test.go file ढूंढता और चलाता है, इसलिए कोई अलग test-discovery configuration की ज़रूरत नहीं।

उदाहरण: Where Tests Live

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("utils/strings.go")
	// Print the values followed by a newline
	fmt.Println("utils/strings_test.go")
}
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. reusable logic को अलग packages में बांटने के बजाय सारा code एक विशाल file में package main में डालना।
  2. पुरानी GOPATH आदत से एक top-level src folder बनाना, जिसकी modern Go modules को ज़रूरत या उम्मीद नहीं।
  3. test files, main application code, और documentation को बिना किसी consistent naming के मिलाना, जिससे 'go build ./...' अप्रत्याशित रूप से व्यवहार करता है।
चैप्टर सारांश
  • एक typical Go project के root पर एक go.mod और entry point के रूप में एक main.go (या एक cmd/ directory) होता है।
  • Reusable logic को छोटे, केंद्रित packages में organize किया जाता है, हर एक अपनी directory में।
  • 'internal/' directory name का एक खास मतलब है: यह imports को module के अंदर तक सीमित करता है।
  • Test files उस code के साथ रहती हैं जिसे वे test करती हैं, _test.go suffix से नामित।
🔒

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.