1 00:00:00,000 --> 00:00:04,481 [MUSIC] 2 00:00:04,481 --> 00:00:08,606 We've seen that Go can store integers, floating point numbers, strings, 3 00:00:08,606 --> 00:00:09,700 booleans and more. 4 00:00:09,700 --> 00:00:13,380 But suppose we add one integer that represents a number of minutes and 5 00:00:13,380 --> 00:00:16,190 another that represents a number of hours. 6 00:00:16,190 --> 00:00:18,130 How do you tell those two apart? 7 00:00:18,130 --> 00:00:22,030 How do you avoid accidentally storing the hours integer in the minutes integer or 8 00:00:22,030 --> 00:00:23,200 vice versa? 9 00:00:23,200 --> 00:00:27,630 Go lets you create custom types that will help you avoid these sort of mix ups. 10 00:00:27,630 --> 00:00:30,430 To create a new custom type you need to declare it. 11 00:00:30,430 --> 00:00:35,300 To do that you use the type keyword followed by the name of your new type. 12 00:00:35,300 --> 00:00:38,860 And then an underlying type that you wanna base your custom type on. 13 00:00:38,860 --> 00:00:43,540 So for example here we have a minutes type that's base on the int underlying type. 14 00:00:43,540 --> 00:00:47,890 Here's another type named hour, that's also based on int. 15 00:00:47,890 --> 00:00:51,030 Here we have a weight that's base on float64. 16 00:00:51,030 --> 00:00:53,979 Titles that are based on the string type, and 17 00:00:53,979 --> 00:00:57,270 an answer type that's based on booleans. 18 00:00:57,270 --> 00:01:01,120 When you define a new custom type that also sets up a conversion 19 00:01:01,120 --> 00:01:03,320 from the underlying type. 20 00:01:03,320 --> 00:01:06,230 So we can use the name of our new custom type and 21 00:01:06,230 --> 00:01:10,040 then do a conversion from a value of the int type. 22 00:01:11,040 --> 00:01:13,590 Once we've created a value of our new custom type, 23 00:01:13,590 --> 00:01:15,260 we can then assign it to a variable. 24 00:01:17,163 --> 00:01:20,540 So here we convert an int to the minutes type. 25 00:01:22,290 --> 00:01:24,549 Here's a conversion from int to hours, 26 00:01:24,549 --> 00:01:27,744 here's a conversion from float 64 to the weight type. 27 00:01:27,744 --> 00:01:31,934 Here's a conversion from a string to the title type, and 28 00:01:31,934 --> 00:01:36,270 here's a conversion from a boolean to an answer. 29 00:01:36,270 --> 00:01:39,530 Then down here we simply print out all our new values. 30 00:01:39,530 --> 00:01:40,600 Let's try running this. 31 00:01:43,670 --> 00:01:47,490 And you see that it simply prints out the underlying values for 32 00:01:47,490 --> 00:01:49,940 each of our custom types. 33 00:01:49,940 --> 00:01:53,620 The underlying type that you base a custom type on will determine what intrinsic 34 00:01:53,620 --> 00:01:56,170 operations your custom type supports. 35 00:01:56,170 --> 00:01:58,940 For example, if you based the minutes type on an int, 36 00:01:58,940 --> 00:02:03,760 it'll support all the same operations that int does, addition, subtraction etc. 37 00:02:03,760 --> 00:02:07,817 If you base the title type on string underlying type, it'll support all 38 00:02:07,817 --> 00:02:12,044 the same operations string does like the concatenation of other strings. 39 00:02:12,044 --> 00:02:15,640 So for example, if we were to take our minutes value. 40 00:02:18,003 --> 00:02:23,340 We could add three minutes onto it by just adding another integer to it. 41 00:02:24,550 --> 00:02:25,920 Let's try printing the result out. 42 00:02:29,680 --> 00:02:31,000 Save that, run it. 43 00:02:32,900 --> 00:02:35,370 And we see that our number of minutes has been updated from 37 to 40. 44 00:02:35,370 --> 00:02:39,954 A value of a custom type can also be compared to 45 00:02:39,954 --> 00:02:43,220 a value of the same custom type. 46 00:02:44,932 --> 00:02:48,180 Or it can be compared to a value of the same underlying type. 47 00:02:50,050 --> 00:02:54,340 So for example, here we take a weight of 945.7 and 48 00:02:54,340 --> 00:02:56,950 we compare it to another weight value. 49 00:02:56,950 --> 00:02:59,060 And down here we compare it to a float value. 50 00:02:59,060 --> 00:03:01,930 And the comparison works in both cases. 51 00:03:01,930 --> 00:03:05,190 It shows that 945.7 is greater than 907.3 and 52 00:03:05,190 --> 00:03:08,970 in both cases it prints capacity exceeded. 53 00:03:11,090 --> 00:03:14,750 Values of a custom type cannot be compared to values of a different custom 54 00:03:14,750 --> 00:03:17,170 type though, even if they have the same underlying type. 55 00:03:18,470 --> 00:03:22,500 So for example if we took a minutes value and an hours value and 56 00:03:22,500 --> 00:03:26,850 tried to compare them to each other, if minutes is greater than hours 57 00:03:29,870 --> 00:03:32,840 If we try running this it wont compile. 58 00:03:32,840 --> 00:03:38,550 We get an error, invalid operation, mismatch types minutes and hours. 59 00:03:38,550 --> 00:03:42,030 You may be wondering what good custom types are if they're just glorified 60 00:03:42,030 --> 00:03:43,860 integers and strings. 61 00:03:43,860 --> 00:03:46,650 There's one more aggregate data type that's more commonly 62 00:03:46,650 --> 00:03:48,828 used in conjunction with custom types. 63 00:03:48,828 --> 00:03:52,670 It's called a struct and it can hold multiple fields of different types. 64 00:03:52,670 --> 00:03:54,540 We'll be covering structs in a little bit, 65 00:03:54,540 --> 00:03:56,900 as well as how to base custom types on them. 66 00:03:56,900 --> 00:04:01,160 But we wanted to show you that a custom type can be based on any underlying type, 67 00:04:01,160 --> 00:04:03,930 even in some strings if you wanted to. 68 00:04:03,930 --> 00:04:07,930 Before we talk about structs we have one more topic to cover, methods. 69 00:04:07,930 --> 00:04:09,770 We'll talk about those in the next video.