Level Up Your Web Apps With Go



Download 3,66 Mb.
Pdf ko'rish
bet34/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

Variable Naming
When naming variables, use camel case starting with a lowercase letter:
myVar := "something" // Good
variable := "something" // Good
MyVar := "something" // Bad
my_var := "something" // Bad
When using an abbreviation, stick to a consistent case depending on the initial
character’s case:
Level Up Your Web Apps With Go
38


id := "123" // Good
myID := "123" // Good
Id := "123" // Bad
myId := "123" // Bad
Constants
In some languages it’s common practice to capitalize constants; however, Go doesn’t
follow this convention, so just use camel case as we do for variables:
const retryTimeout = 60 // Good
const RETRY_TIMEOUT = 60 // Bad
const Retry_TIME_OUT = 60 // Worst
Constructors
The last aspect to note is the way that new instances of types are initialized. Rather
than having the concept of a
constructor
as many languages do, the convention is
to have a function that starts with
new
or
New
(depending on whether you want to
export or not), followed by the type. This function would generally take whatever
information is absolutely required for a working type as its parameters. For example,
with our earlier
Movie
type, we may have a
NewMovie(title string, year int)
Movie
function because we only need the title and year. Other values can be added
later:
type Movie struct {
Actors []string
Rating float32
ReleaseYear int
Title string
}
func NewMovie(title string, year int) Movie {
return Movie{
Title: title,
ReleaseYear: year,
39
Go Types Explored


Actors: []string{},
}
}
Notice how we created an empty string array for the
Actors
? These new functions
provide the opportunity to initialize empty structures that the type requires to
function, and it’s common practice to initialize empty arrays or maps within the
new function.
Summary
If you’ve made it this far, you’ve pretty much covered the entire fundamentals of
how to write Go programs! In the next chapter, we’re going to have a look at some
web development-specific coding. We’ll also cover how to handle HTTP requests
and responses, how Go handles templates, and how to write and read JSON.
Level Up Your Web Apps With Go
40



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