Go Project की Workspace Structure
In this page:
A Minimal Layout
सबसे simple संभव Go project बस एक ही directory में एक go.mod file और एक main.go है। जैसे-जैसे project बढ़ता है, code को subdirectories में बांटा जाता है, हर subdirectory अपना import होने योग्य package बनता है जिसका नाम उसके folder पर होता है।
उदाहरण: A Minimal Layout
// 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")
}
Login to try C/C++/Java/PHP code in the editor
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 करना आसान बनाता है।
उदाहरण: Organizing Packages by Folder
// 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)
}
}
Login to try C/C++/Java/PHP code in the editor
The internal/ Convention
Go internal नाम की directory के अंदर किसी भी package को खासतौर पर treat करता है: इसे सिर्फ उस internal directory के parent पर rooted code ही import कर सकता है।
यह packages को private implementation details के रूप में चिह्नित करने का standard तरीका है जिन पर बाहरी modules को निर्भर नहीं रहना चाहिए।
उदाहरण: The internal/ Convention
// 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")
}
Login to try C/C++/Java/PHP code in the editor
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
// 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")
}
Login to try C/C++/Java/PHP code in the editor
- reusable logic को अलग packages में बांटने के बजाय सारा code एक विशाल file में package main में डालना।
- पुरानी GOPATH आदत से एक top-level src folder बनाना, जिसकी modern Go modules को ज़रूरत या उम्मीद नहीं।
- 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: