Pichle post mein humne Go ke baare mein jaana tha.
Ab Go ka setup karenge aur pehla Go program likhenge!
Pehle Magajmari (Setup)
Hame bas 2 cheese chahiye:
- Ek Go
- Dusra Code Editor
Official website pe jao aur apne platform ke hisaab se Go download kar lo.
Rahi code editor ki baat, to VS Code aur Go ka extension install kar lo.
Bas!
Chela: Guruji! Linux pe Go ka package kaha rakhna hai aur PATH environment variable kese set karte hai?
Guru: Baccha! Tu Go seekhne aya hai ya Google karna?
Chela: 😅 Bas iss baar bataado 🙏
Guru: Hmm.
Linux pe Go
Terminal kholo aur shuru ho jao!
- Sabse pehle purna koyi Go installation ho to use uthake phenk do! Taaki version conflict naa ho.
sudo rm -rf /usr/local/go
- Phir apne platform ke hisaab se package ko download karo (latest wala).
wget https://go.dev/dl/go1.26.5.linux-amd64.tar.gz
- Ab us package ko unpack ya extract karo
/usr/local/me
sudo tar -C /usr/local -xzf go1.26.5.linux-amd64.tar.gz
- Bas ab Go ka path set karna hai.
Ye neeche wala command
.bashrcfile me Go ka path set kardega!
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
- Ya to naya terminal tab kholo ya phir ye karo:
source ~/.bashrc
Ab Go ka version print karke dekho:
go version
Thoda Gyan
Kuch bhi likhne se pehle Go ke kuch niyam jaan lete hai!
Go ka code ham
.gowale files me likhte hai (.goextension)
Ek example Go project ka structure kuch aesa dikhta hai
To, niyam:
- Har .go wala file ek folder ka hissa hota hai jise ham package kehte hai
- Jitne bhi hamare package wale folder hai wo sab ek bade folder ke hissa hai, jise ham module bolte hai
- Ek Go module yaani ek project hai, matlab ya to module ko ek library ki tarah dusre projects me istemal kar sakte hai ya phir iss module se ek binary executable bana sakte hai
- Ek folder ko module tabhi bolte hai jab usme go.mod file ho! Warna go compiler chillake bolega "Ye module nahi hai! Me compile kese karu?"
Yaha 2 sawaal manme ayenge:
- go.mod file ka kaam kya hai?
- Jab saare .go files kisi na kisi package folder ke ander honge to uper image me main.go file seedha module wale folder me kese ghum raha hai?
Aur unke javaab kuch ese hai:
- go.mod file external dependencies ko manage karta hai. Yaani ki agar hamare project me ek web framework ya koi bhi library chahiye kisi dusre ka likha hua, to uska zikr hum go.mod file me karenge aur go compiler use project me add kar dega!
- main.go, wese to uska naam kuch bhi rakh sakte hai par uske ander ham bolte hai ki ye main package ka hissa hai.
- main package hamare module(project) ka entry point hai, jaha se execution shuru hota hai! Iss liye wo seedha module wale folder ke ander ghum sakta hai
- Ek aur baat, ham jitne chahein utne .go files bana sakte hai main package ke ander, matlab module wale folder me!
Bas har file ke ander kehna hoga ki ye main package ka hissa hai, aur ek file esa hona chahiye jisme main() naam ka function ho!
Hello World Program
Terminal ko apne pasand ke location pe kholo aur shuru!
- Project ka folder banake usme ghuso
mkdir hello && cd hello
- Go se kaho ki wo go.mod file banaye
go mod init hello
- Ab ek main.go file banao
touch main.go
- main.go ko VS Code me kholo
code main.go
- main.go ko neeche wale code se bhar do
package main
import "fmt"
func main(){
fmt.Println("Pranaam 🙏🙏🙏🙏")
}
- Ab file ko save karne ke baad wapis terminal me aake project ko run karo
go run .
Aapke terminal ke screen pe Pranaam 🙏🙏🙏🙏 wala message dikha to aapne sab tikh se kar diya!
Agar nahi dikha to kya karna hai mujhe nahi pata 😄
(Google karke dekhlo)
Iss program ka matlab kya hai ye hum agle post me dekhenge!
Tab tak ke liye alvida!


Top comments (0)