go run और go build Commands
go run file.go
go build [-o output] [package]
Using go run
'go run' आपके code को एक temporary location में compile करता है और इसे एक single command में चलाता है, फिर binary को discard कर देता है। यह development के लिए सबसे तेज़ feedback loop है क्योंकि आपको कभी intermediate executable file के बारे में सोचने की ज़रूरत नहीं।
उदाहरण: Using go run
go run main.go
⚠️ Run this in your own terminal or Node.js environment.
Using go build
'go build' आपके program को compile करता है और current directory में एक असली executable file लिखता है, जिसका नाम default रूप से module या source file के नाम पर होता है।
यह binary एक पूरा, self-contained program है -- यह Go runtime को embed करता है, इसलिए इसे चलाने वाली machine को Go installed होने की ज़रूरत नहीं।
उदाहरण: Using go build
go build main.go
⚠️ Run this in your own terminal or Node.js environment.
Naming the Output with -o
-o flag आपको बिल्कुल चुनने देता है कि compiled binary कहां जाए और इसका नाम क्या हो, जो build scripts और CI pipelines में उपयोगी है जहां एक predictable filename मायने रखता है।
उदाहरण: Naming the Output with -o
go build -o myapp main.go
⚠️ Run this in your own terminal or Node.js environment.
Running the Compiled Binary
Build होने के बाद, आप binary को अपने system पर किसी और program की तरह सीधे execute करते हैं।
यह two-step build-then-run workflow वह है जिसे आप deploy या distribute होने वाली किसी भी चीज़ के लिए उपयोग करेंगे, 'go run' के विपरीत जो सिर्फ local iteration के लिए है।
उदाहरण: Running the Compiled Binary
./myapp
⚠️ Run this in your own terminal or Node.js environment.
- Production deployment scripts में 'go build' से एक बार binary बनाने और उसे चलाने के बजाय 'go run' उपयोग करना।
- यह उम्मीद करना कि 'go build' program execute करेगा -- यह सिर्फ compile करता है; आपको resulting binary खुद चलानी चाहिए।
- यह न समझना कि 'go build' output binary default रूप से package/module name लेता है, जिससे confusion होता है कि कौन सी file produce हुई।
- 'go run file.go' किसी binary को पीछे छोड़े बिना program compile और तुरंत execute करता है।
- 'go build' program को disk पर एक standalone executable file में compile करता है।
- 'go build -o name' आपको output binary का नाम control करने देता है।
- Built binaries को compatible OS/architecture पर चलने के लिए किसी external Go installation की ज़रूरत नहीं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: