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

आपका पहला Go Program

आपका पहला Go program instructions का एक छोटा set है जो computer को screen पर एक greeting print करने के लिए कहता है।
Syntax
go
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 करेगा।

Note: Library code (import होने के लिए, सीधे चलाने के लिए नहीं) एक अलग package name उपयोग करता है, जैसे 'package mathutils'।

उदाहरण: The package main Declaration

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("package main marks this as a runnable program")
}

The main Function

package main के अंदर, Go बिल्कुल main नाम का, बिना parameters और बिना return value वाला एक function ढूंढता है -- यहीं से execution शुरू होता है।

jo कुछ भी program चलाने पर होता है वह इसी single function के अंदर से trigger होता है, या इसी से पहुंचा जा सकता है।

उदाहरण: The main Function

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("Execution starts here, inside func main()")
}

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 में लाते हैं।

Note: Go में एक unused import सिर्फ एक linter warning नहीं बल्कि एक compile-time error है -- यह codebases को dead imports से मुक्त रखता है।

उदाहरण: Importing and Using fmt

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 name and infer the type
	name := "Gopher"
	// Print using a format string
	fmt.Printf("Hello, %s! Welcome to Go.\n", name)
}

Running Your Program

'go run' command आपकी source file को एक temporary binary में compile करता है और तुरंत इसे execute करता है, output को आपके terminal में print करते हुए। यह सीखते समय build artifacts को manually manage किए बिना छोटे programs आज़माने का सबसे तेज़ तरीका है।

Note: 'go run' तेज़ iteration के लिए बढ़िया है; जब आपको एक reusable binary file चाहिए तब 'go build' उपयोग करें।

उदाहरण: Running Your Program

bash
go run hello.go

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

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. file के package को main के अलावा कुछ और नाम देना और फिर confuse होना कि 'go run' कोई executable क्यों नहीं produce करता।
  2. func main() entry point भूल जाना -- Go programs package main में बिल्कुल एक main function के बिना compile नहीं होंगे।
  3. 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:

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.