0 ratings 0% found this document useful (0 votes) 9 views 5 pages GO Unit-3b
The document discusses the use of embedded types and structs in Go, highlighting how they can be utilized to implement interfaces for flexible and maintainable code. It explains the implications of different initialization methods for structs, including default initialization with zero values and explicit initialization. Additionally, it emphasizes the implicit implementation of interfaces by structs, which allows for reduced dependencies and increased reusability.
AI-enhanced title and description
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
Save GO unit-3B For Later : Brdunt( 8)
Whe is Method ovesording worth Embedded ty pes?
How PO interfaces and Embed ding wosk togethes ©
: abo fi é x -
DY explain hhow Go emplys embedded ty pes us ithi 9
Strucks +5 AduTeve Covapositron over inhewitan ce. +
Disuas the dif ways a Stuck dan be fruit oalred
3 Go swWhet ave the Imp ications of each methal
with pespeet 4o2e%0 values? ea ae
@r- Different ways a St can be Inibralized 79 he-
> Tn Gw ) there dve Several ways ag Tal
Strmtk: J
~) Gack method | hos dlf Imp cotions 5 espeeca lly
edtian HE tomes 48 zevo! my ahuegh ie slo ;
balive o
@ defautt Tri talization with zero values
— SS
2 ™ be obenyou declare g Stat Variable
: usithoud explicitly, initvabizing it frelds |
aly Peids awe o4Sished their 2er0 are
pe 2. vo deyee Person Sud 3 :
Name Stving Bd &
ey Age. ine ena ye
1 Mol p Pensonei ee
gon prcadions!
ace Hees ave sel ty there zexo’ eh toa
(empty Sing) rand age isd. Beet
anda ;
Ko but may lead “to #9025 tf
ahis method is tard
Lninitielied frerds ae used -evithout
chet, Values.
5
checking
chon} :
estud with a struct Literal by
@ Uterst in
3 you Can
speetiying wnleet for tne. ov niin.
me Spectyig. BUC Rerdsy)
be Peosond
bo Mame "ALE",
ag RIE
Implications; Fi
son specrked fields are initsalie & istth provided Valuss.
not Specified ave automatically fet te
ao tny, Fes
tyes, pero values
b pastiatly specttying Fists:
pia Perso} ss
Name alice", .
mpls Gbions- ms oe .
Bonty Name is taitalired te Alice > and pge Fema
ob Ht 2270 Mtalucl)- -
whan gou Ware ty initalite, Some. fre lets
‘ 2 20% valu
pelawrs the
coroactas’ a pointer cho ostru CE
——
7 uting whe news Keyes
with 2270 etiacs for beds
2S neolPer) 6 : 2
jrepUcatfons- Caen
’ oeeare a pointey, Father than a pexsor suck
ave initialized "+t their 2230 valacs
> an detds
@ using o_con shru chor function ;
Spithorsh Go clecin't' Support: coristrittes natively,
ge tan dene a Constra thor fon , ty det
Griblal valets
oot aha ag
he beiC
€' fave aopeson (name Sting 1456 ink) peatong
vetien person EName wna’ y Age = osey
Neo ferwon(-* a tice", 30).
Treplicecttons!
~DATIOWS you te endbwee
certain’ fetds... i eae
“engaure att felds are &4, upon: jripebratoo. ,
2 Pre lds.
avuvidin§ 7p any unintended penovalue Fre lds
Or Method ves viding >
_Method_ ove"
ypuwhen a Mmtthod 1S over?
uoith Same name and Receiver type oF an CSting
method: iS Weeted anit is used fy Place. of
.%
Sonuirad, Pee
tdden fo ‘the, & rdw Crethed
Segitsting . ene. a
Asa wesutt » Golang may Powsile paijaoctteom
7 During the embedding. .pwcess. 7, Ge ois 6
marked ts be overriden «
Cramples> suppose you have 400 structs’;
won + person and Lm ploy 22 roskere emtelete
embeds ‘person. =
paleose moth a : ee
Preport “Pmt” a pnade 25h
type person Steuct Jo * Feand
ix Name Shing < BA ane
3 4 : OA. -aagpe Rg
Func (p Pe%on) Greto7
é
Frc prindln (Helle, namiis pnamé)
Bis wee Ce amelogec) Greet ¢),
fmt: printe
3
a main 61 &
3 toyee ( pers “ " “ i
es emia Peron &" Alice}, “revetoper"terompte Lolth Totedacesy
on ii
cj anterdace fh Gw 7s Blac print of a behaviour
-) antey face Contains — gtadic constants and abthrach
method .
Ex: Pecan main
import fmt"
-Lype Cywecd er interface b
Creel 6)
vi
type person Struct db
Alame Shing
anc (p person) qrect C98
fend: -println C'Heto", p-Newme )
tyee Employee Stouct &
. Person Strivg
func fe employee) Greet © 0&
Bent. printiln ("HEH cy <- positien)
fan thon maint d&
vol g greater
= employee (person d Natice 4 y"pevelopes”
3- greet l )
Ae
oo
_———*Discuss how structs can be used to implement?
interfaces and why this can be a powerful way to
fol SS Tea}
flexible and maintainable code
(ene ES 1eH
ChatGPT
In Go, structs and interfaces work together to
VR) [MCI ro) LM aT-L 1 eselel
by using composition rather than inheritance.
This composition-based approach allows
developers to design code with interchangeable
parts, making it easier to build, extend, and test
functionality. Here's how and why this pattern is
so effective:
1. Structs Implement Interfaces Implicitly
In Go, there's no need to explicitly declare that
ERS anno) (ne n) leem Cle)
struct has the required methods of an interface,
it automatically satisfies that interface. This
implicit implementation reduces dependencies
between packages and allows structs to be more
flexible and reusable.
> <-Tan) oo
go
Copy code
aL Mele CM CIEL
Greet() string
a
Damo uaS
Name string
}
func (p Person) Greet() string {
return "Hello, " + p.Name
a
Ae eR UC Cle
var g Greeter = Person{"Alice"}
fmt.PrintIn(g.Greet()) // Output: Hello, Alice
Here, Person automatically satisfies the Greeter
interface because it has a Greet method. This
implicit implementation allows Person to be
used wherever a Greeter is expected, making it
versatile.