आपका पहला Go Program
In this page:
package main
import "fmt"
func main() {
fmt.Println("text")
}
The package main Declaration
हर Go source file एक package declaration से शुरू होती है। एक standalone, चलने योग्य program बनाने के लिए, file को 'package main' से belong करना चाहिए -- यह Go tool को बताता है कि यह package एक reusable library के बजाय एक entry point है।
अगर आप इसे कोई और नाम देते हैं, तो 'go build' एक executable के बजाय एक library archive produce करेगा।
उदाहरण: The package main Declaration
// 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("package main marks this as a runnable program")
}
Login to try C/C++/Java/PHP code in the editor
The main Function
package main के अंदर, Go बिल्कुल main नाम का, बिना parameters और बिना return value वाला एक function ढूंढता है -- यहीं से execution शुरू होता है।
jo कुछ भी program चलाने पर होता है वह इसी single function के अंदर से trigger होता है, या इसी से पहुंचा जा सकता है।
उदाहरण: The main Function
// 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("Execution starts here, inside func main()")
}
Login to try C/C++/Java/PHP code in the editor
Importing and Using fmt
fmt package (format का संक्षिप्त रूप) Go की standard library का हिस्सा है और formatted input/output के लिए functions प्रदान करता है, सबसे आम तौर पर एक line print करने के लिए fmt.Println और %s या %d जैसे format verbs के साथ print करने के लिए fmt.Printf। आप इसे file के ऊपर एक import statement से scope में लाते हैं।
उदाहरण: Importing and Using fmt
// 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 name and infer the type
name := "Gopher"
// Print using a format string
fmt.Printf("Hello, %s! Welcome to Go.\n", name)
}
Login to try C/C++/Java/PHP code in the editor
Running Your Program
'go run' command आपकी source file को एक temporary binary में compile करता है और तुरंत इसे execute करता है, output को आपके terminal में print करते हुए। यह सीखते समय build artifacts को manually manage किए बिना छोटे programs आज़माने का सबसे तेज़ तरीका है।
उदाहरण: Running Your Program
go run hello.go
⚠️ Run this in your own terminal or Node.js environment.
- file के package को main के अलावा कुछ और नाम देना और फिर confuse होना कि 'go run' कोई executable क्यों नहीं produce करता।
- func main() entry point भूल जाना -- Go programs package main में बिल्कुल एक main function के बिना compile नहीं होंगे।
- file में एक ऐसे package के लिए import statement छोड़ना जो कभी उपयोग नहीं होता, जिसे Go एक warning नहीं बल्कि एक compile error मानता है।
- हर चलने योग्य Go program 'package main' में रहता है और अपने entry point के रूप में एक 'func main()' define करता है।
- fmt package output print करने के लिए Println और Printf जैसे functions प्रदान करती है।
- Go को हर imported package असल में उपयोग होनी चाहिए, नहीं तो program compile नहीं होगा।
- 'go run file.go' एक step में एक program compile और execute करता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: