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

go run और go build Commands

'go run' किसी program को एक बार test-drive करने जैसा है, जबकि 'go build' इसे एक असली, saveable file में बेक कर देता है जिसे आप किसी और को दे सकते हैं।
Syntax
go
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

bash
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 होने की ज़रूरत नहीं।

Note: Windows पर produce की गई file को अपने आप एक .exe extension मिलता है।

उदाहरण: Using go build

bash
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

bash
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 के लिए है।

Note: ./ से prefix करना shell को बताता है कि PATH search करने के बजाय file को current directory से चलाए।

उदाहरण: Running the Compiled Binary

bash
./myapp

⚠️ 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. Production deployment scripts में 'go build' से एक बार binary बनाने और उसे चलाने के बजाय 'go run' उपयोग करना।
  2. यह उम्मीद करना कि 'go build' program execute करेगा -- यह सिर्फ compile करता है; आपको resulting binary खुद चलानी चाहिए।
  3. यह न समझना कि '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:

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.