Hi there Serdar Yegulalp here for Infoworld at IDG in this episode of Smart Go I'm going
to give you a quick tour of the basic data types in the go language and how variable
assignments work with them every language for computing has a set of basic variable types
these are the building blocks out of which other types can be composed in go the basic types ought
to be familiar to anyone who's come from a low-level language like c since many of ghost
types are meant to reflect what's available on the hardware of most computer platforms
the most basic data type in a computer is a bit or a boolean as it's called that's one or zero
true or false so in go we have the bool type which can be either true or false
up there we have the integer types and go you have two basic kinds of integers
the signed integer int and the unsigned integer uint the default size for both of those
is whatever the bitness is of the underlying operating system typically that's 64-bit since
most of us are running 64-bit systems at this point if you want an integer of some specific
bitness you could request an 8 16 32 or 64-bit variety of integers just by changing the name
if you need integers that are bigger than what a 64-bit number can hold go has a package in
its standard library math big that deals with such things these are what are called arbitrary
precision numbers and that's something worth covering in a separate video though
for floating point numbers go has two built-in types float 32 and float 64.
there are third-party packages for a float16 type since there are some
data science applications that now make use of that format but ghost maintainers don't plan
to add float16 to the language itself unless there's widespread hardware support for it
huko also has two data types for complex numbers complex 64 and complex 128 which are used for
float 32 and float64 numbers respectively and you can describe these by way of a function complex
10 comma 4 or by way of a special addition syntax 10 plus 4i both of these will yield the same value
go also has a distinct type for holding memory addresses uint ptr this is just an integer which
is the size of the underlying platform's bit depth so for a 64-bit platform it's 64 bits
it is worth noting that a uint pointer as it can also be called is not itself used as a
pointer you can't dereference it and get the contents of memory with it it's just a number
that means you can perform math on it which is something that you cannot do with an actual go
pointer this distinction is something worth going into in another video but it's useful
to know that we have a special type for this sort of thing another type in go that ought to
be familiar to people coming from c is the byte type this is just an alias for an unsigned 8-bit
integer however unlike in c there is no char or character type and go finally on that note
there are two variable types that can be used for dealing with strings the string type and the rune
type strings are just immutable text stored as an area of bytes but runes are individual unicode
code points they're essentially in 32s that are used specifically for working with individual
unicode characters again the way strings and runes work and go is complex enough that it's
worth talking about another video now in go every variable that you use has to be declared in some
form but you do have some flexibility with how declarations work so there's a number of different
syntaxes for variable declarations now if for instance i just say a equals true goes compiler
will reject it because if i use an equal sign by itself that just implies that a has already been
declared somewhere so if the compiler can't find that declaration for it it won't assume anything
and it will just refuse to compile the program so to make this work we can just say var a bool
and that will explicitly declare the variable we can also say var a bool equals true or just var a
equals true since it's unambiguous what type a would be in this case
but we can also use the colon equals operator to declare and assign a variable in one swoop
this is the most compact of the syntaxes that we can use but it makes the most sense and
is the least confusing in contexts where the variable scope is small for instance
if you're using it in a for loop that's where the sort of thing is most commonly used anyway
lastly one convenient thing about variable declarations which is that if you declare
but don't assign a variable it always gets set to a same default for numbers that's zero
for booleans it's false for strings it's a zero length string this way you don't have to worry
about catastrophic consequences if something isn't actually initialized that's it for this video if
you liked it please leave a comment below don't forget to subscribe to the id2 tech talk channel
on youtube and for more smart go be sure to follow us on facebook youtube and infoworld.com