1 00:00:00,360 --> 00:00:01,930 Go is statically typed. 2 00:00:01,930 --> 00:00:05,990 This means that Go knows what the types of your values are, even at compile time. 3 00:00:05,990 --> 00:00:08,750 And it can tell whether they're being used appropriately. 4 00:00:08,750 --> 00:00:10,400 For example, in this program here, 5 00:00:10,400 --> 00:00:14,820 we try to add the number 527 onto the string hello. 6 00:00:14,820 --> 00:00:18,360 And before our program even compiles, we get an error. 7 00:00:18,360 --> 00:00:21,610 Saying, cannot convert the string hello to the type int. 8 00:00:21,610 --> 00:00:27,480 And, this operation is invalid, trying to add 527 to the value hello. 9 00:00:27,480 --> 00:00:29,740 In some languages this would fail at run time, 10 00:00:29,740 --> 00:00:32,810 possibly while you demoing your software to a user. 11 00:00:32,810 --> 00:00:34,910 In Go, it fails at compile time, so 12 00:00:34,910 --> 00:00:38,040 your development team knows about the problem immediately. 13 00:00:38,040 --> 00:00:40,020 There are many built in types for numbers, 14 00:00:40,020 --> 00:00:43,160 most of which are used only in special circumstances. 15 00:00:43,160 --> 00:00:46,190 You should know they exist, so you're not surprised by them later. 16 00:00:46,190 --> 00:00:48,310 So I'll describe them to you briefly, but 17 00:00:48,310 --> 00:00:50,260 then you can forget all about most of them. 18 00:00:50,260 --> 00:00:53,110 Because they're really only two numeric types you're 19 00:00:53,110 --> 00:00:55,160 going to use on a regular basis. 20 00:00:55,160 --> 00:00:59,890 For integers you have int, int8, int16, int32 and int64. 21 00:01:00,910 --> 00:01:05,240 The numbers at the end of int8, int16, etc, are the number of bits, ones and 22 00:01:05,240 --> 00:01:08,080 zeroes, that are used to store the number in memory. 23 00:01:08,080 --> 00:01:11,870 The more bits a number takes up, the larger its maximum value can be. 24 00:01:11,870 --> 00:01:15,567 int8 can only hold a maximum value of 127. 25 00:01:15,567 --> 00:01:18,990 You'll get an error if you try to treat a larger number as an int8. 26 00:01:18,990 --> 00:01:23,410 But int64 can hold a maximum value of over 9 quintillion. 27 00:01:23,410 --> 00:01:26,970 Of course, using more bits means using more of your computer's memory. 28 00:01:26,970 --> 00:01:29,058 But computers have so much RAM these days, 29 00:01:29,058 --> 00:01:31,726 it's generally not worth it to use the smaller types, 30 00:01:31,726 --> 00:01:35,454 since the risk of errors is increased from using a number that's too large. 31 00:01:35,454 --> 00:01:38,860 The int types can hold negative or positive numbers, 32 00:01:38,860 --> 00:01:42,290 but the uint types can hold only positive numbers. 33 00:01:42,290 --> 00:01:45,405 Again, unless you have a specific reason to use a uint, 34 00:01:45,405 --> 00:01:47,720 it's probably not worth the additional effort. 35 00:01:47,720 --> 00:01:50,099 You can just use a int type instead. 36 00:01:50,099 --> 00:01:53,600 int and uint types can only hold whole numbers. 37 00:01:53,600 --> 00:01:54,910 For floating point numbers, 38 00:01:54,910 --> 00:01:59,450 that is numbers with a decimal point, you'll want the float32 or float64 types.. 39 00:02:00,660 --> 00:02:04,110 But as I said, you don't need to remember most of those numeric types. 40 00:02:04,110 --> 00:02:08,188 By default, Go treats whole numbers as being of the int type, which is 41 00:02:08,188 --> 00:02:12,974 the same as the int64 type, unless you're on an old 32 bit operating system. 42 00:02:12,974 --> 00:02:16,710 And floating point numbers are treated as the float64 type. 43 00:02:16,710 --> 00:02:19,940 We can confirm this by importing the reflect package and 44 00:02:19,940 --> 00:02:24,310 calling its type of function on an integer and on a floating point number. 45 00:02:24,310 --> 00:02:27,620 If we run this, it will output int for the integer and float64 for 46 00:02:27,620 --> 00:02:29,610 the floating point number. 47 00:02:29,610 --> 00:02:33,580 If you're using a different numeric type and you wanna do math or comparisons with 48 00:02:33,580 --> 00:02:38,380 hard coded numbers, you'll have to convert your values to int or float64 first. 49 00:02:38,380 --> 00:02:41,320 So unless you have a particular reason to do otherwise, you'll find 50 00:02:41,320 --> 00:02:45,220 it's much more convenient to just use the int and float64 types in your programs. 51 00:02:46,320 --> 00:02:53,284 Now let's add a string and a Boolean value and see what types those are. 52 00:02:53,284 --> 00:02:56,750 So I'll say, reflect.TypeOf and we'll add a string, hello. 53 00:02:59,028 --> 00:03:03,040 And then reflect.TypeOf and we'll do a Boolean value, false. 54 00:03:04,600 --> 00:03:05,650 Save that, rerun it. 55 00:03:06,800 --> 00:03:10,950 And we can see that in addition to the ints and the float64 from before, 56 00:03:10,950 --> 00:03:15,010 we have a string, and a bool value, short for Boolean. 57 00:03:15,010 --> 00:03:17,710 Those are the most frequently used built in types. 58 00:03:17,710 --> 00:03:20,283 It's also possible to define additional types. 59 00:03:20,283 --> 00:03:24,520 The Go Standard Library includes several packages that define types of their own. 60 00:03:24,520 --> 00:03:28,850 For example, if we were to import the net and time packages, and 61 00:03:28,850 --> 00:03:34,105 then create a new net.IPv4 instance, 62 00:03:34,105 --> 00:03:39,330 or a new time object, and then print the types of those objects, 63 00:03:39,330 --> 00:03:45,230 we would get net.IP as the output as well as time.Time. 64 00:03:45,230 --> 00:03:47,600 Those are the types of those values that get created