← Back to Go Course | Chapter 10: Packages & Modules | Lesson 6 of 6

Exported और Unexported Identifiers में अंतर

Go में किसी नाम को capitalize करना उस पर एक PUBLIC sign लगाने जैसा है -- lowercase से शुरू होने वाली कोई भी चीज़ चुपचाप अपने package के लिए PRIVATE रहती है।
Syntax
go
func Exported() {}    // capital letter: visible outside package
func unexported() {}  // lowercase: package only

Exported Identifiers

कोई भी top-level function, type, variable, या constant जिसका नाम uppercase letter से शुरू हो exported है, यानी दूसरे packages में code package import करने के बाद इसे access कर सकता है।

उदाहरण: Exported Identifiers

markup
package main

import "fmt"

type Product struct {
	Name string // exported field
}

func NewProduct(name string) Product {
	return Product{Name: name}
}

func main() {
	p := NewProduct("Widget")
	fmt.Println(p.Name)
}

Unexported Identifiers

lowercase letter से शुरू होने वाला नाम unexported है, इसे उस package तक सीमित करते हुए जिसमें यह declared है -- यह Go का access control का पूरा mechanism है, बिना किसी private या public keywords की ज़रूरत के।

उदाहरण: Unexported Identifiers

markup
package main

import "fmt"

type product struct {
	name string // unexported field, only usable within this package
}

func main() {
	p := product{name: "Widget"}
	fmt.Println(p.name)
}

Exporting Struct Fields Selectively

एक struct exported और unexported fields मिला सकता है, आपको सिर्फ external उपयोग के लिए intended हिस्से उजागर करने देते हुए जबकि internal bookkeeping fields को package के लिए private रखते हुए।

Note: invariants enforce करने के लिए जिन्हें direct field access bypass कर देगा, unexported fields plus exported constructor/accessor functions उपयोग करें।

उदाहरण: Exporting Struct Fields Selectively

markup
package main

import "fmt"

type Account struct {
	Owner   string // exported
	balance float64 // unexported: only this package can read/modify it directly
}

func NewAccount(owner string, initial float64) Account {
	return Account{Owner: owner, balance: initial}
}

func (a Account) Balance() float64 {
	return a.balance
}

func main() {
	acc := NewAccount("Tara", 500)
	fmt.Println(acc.Owner, acc.Balance())
}
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. किसी दूसरे package से एक lowercase (unexported) identifier access करने की कोशिश करना और compile error से confuse होना।
  2. 'सुरक्षित रहने के लिए' default रूप से हर single identifier को capitalize करना, internal details उजागर करते हुए जो private implementation रहनी चाहिए।
  3. यह मान लेना कि exported/unexported किसी package के अंदर भी लागू होता है -- ऐसा नहीं है; सभी identifiers एक ही package की हर file को case की परवाह किए बिना दिखाई देते हैं।
चैप्टर सारांश
  • uppercase letter से शुरू होने वाला identifier exported है -- दूसरे packages में code को दिखाई देता है।
  • lowercase letter से शुरू होने वाला identifier unexported है -- सिर्फ अपने package के अंदर दिखाई देता है।
  • यह capitalization rule functions, types, variables, constants, और struct fields सभी पर लागू होता है।
  • Unexported identifiers अब भी एक ही package की हर file को पूरी तरह दिखाई देते हैं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.