Level Up Your Web Apps With Go


Syntax Options and Conventions



Download 3,66 Mb.
Pdf ko'rish
bet32/37
Sana01.05.2022
Hajmi3,66 Mb.
#600662
1   ...   29   30   31   32   33   34   35   36   37
Bog'liq
level up your web apps with go

Syntax Options and Conventions
Go is quite opinionated when it comes to how you should write your code; in fact,
it has some very firm rules in place for this. Some formatting is enforced by the
compiler, while some is convention.
Alternative Syntax Options
We’ll look at a few syntax options here, namely ones that you might want to use in
day-to-day programming, or come across in libraries you use.
Initializing Structs without Field Names
You can initialize struct types without specifying the field names. To do this, you
have to pass in all the fields in the same order in which they’re defined in the type
definition; otherwise, you’ll receive an error when compiling. Use this method
35
Go Types Explored


where you can fit the values on one line and the number of fields in the struct are
unlikely to change:
type Human struct {
Name string
Age int
}
// Valid initialisation
me := Human{"Mal Curtis", 29}
// Error: cannot use 29 (type int) as type string in field value
// Error: cannot use "Mal Curtis" (type string) as type int in 
// field value
me = Human{29, "Mal Curtis"}
// Error: too few values in struct initializer
me = Human{"Mal Curtis"}
// Error: mixture of field:value and value initializers
me = Human{"Mal Curtis", Age: 29}
Empty Variable Initialization
So far we’ve only been initializing variables by assigning them with
:=
. You can
also explicitly declare a variable without assigning a value to it by using the
var
keyword followed by the variable name and its type:
var myString string // myString is an empty string
myString = "Hello!" // We don’t need to use `:=` here
When initializing this way, the value assigned will be the "empty value" for the
type. For number types this will be
0
, and strings receive an empty string. Structs
will be initialized, with all fields set to their empty values too. A pointer type will
be set to nil, as will slices and maps:
var myMap map[string]string // myMap is nil
myMap["Test"] = "Hi" // panic: assignment to entry in nil map
You will still need to assign a map to the variable, but the variable’s type will stay
the same:
Level Up Your Web Apps With Go
36


myMap = map[string]string{}

Download 3,66 Mb.

Do'stlaringiz bilan baham:
1   ...   29   30   31   32   33   34   35   36   37




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish