Last active
October 12, 2017 17:11
-
-
Save inancgumus/23d358191bf1c76cefa574dd94738b12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
// Sleep func let us stop the progress for a duration | |
// | |
// func Sleep(d Duration) | |
// | |
// see: https://golang.org/src/time/sleep.go | |
// Duration is defined as a new type in time package as an int64 | |
// | |
// type Duration int64 | |
// | |
// see: https://golang.org/src/time/time.go#L620 | |
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
// # LESSON-1 | |
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
// QUESTION: | |
// How can we pass a value to `time.Sleep` without using a `Duration` type? | |
// When it's only accepting a `Duration` type? | |
// | |
// ANSWER: | |
// Use an untyped numeric constant. | |
// | |
// SOLUTION: | |
// `const delay` is an untyped constant and its type lives in the | |
// ideal-types space. It's not a usual integer (int, int32, int64, etc.). | |
const delay = 2000000000 // untyped const | |
// As you can see at line #114, we can pass this untyped-const to `time.Sleep` | |
// even it expects a Duration type. This is because, `delay` const is a | |
// numeric ideal-type, not an int or float64. | |
// | |
// So, even `time.Sleep` expects a `Duration` type we can pass `delay` to it. | |
// This is because, `Duration` is a numeric type which is `int32`. | |
// | |
// When we pass `delay` to `time.Sleep`, the compiler will convert our | |
// `delay` into an `int32` type and pass that to `time.Sleep` automatically. | |
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
// # LESSON-2: | |
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
// Try to comment `const delay` above, by adding // before const | |
// and uncomment the `delay := 2000000000` below to experience the error | |
// delay := 2000000000 // typed variable | |
// Try to uncomment the above line by removing the left-most // | |
// EXPLANATION: | |
// We used a short-variable declaration here to create a `delay` "variable", | |
// not a constant. So, it has an usual type of int64. And, because, Go has | |
// a strong type system, the compiler won't allow us to use it as a | |
// parameter to `time.Sleep`. Because, `time.Sleep` only accepts a | |
// `Duration` type. | |
// | |
// One solution is to convert the delay into a `Duration` to supress the | |
// error. But, this is only fixing the symptom, not the actual cause. | |
// So, it's better to use an untyped-constant here. | |
// SOLUTION: | |
// Use casting: | |
// | |
// delay := time.Duration(2000000000) // see the verbosity? | |
// | |
// now `delay` has `Duration` type. | |
// SOLUTION: | |
// Use a typed variable: | |
// | |
// var delay time.Duration = 2000000000 // see the unnecessary variable type? | |
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
// # LESSON-3: | |
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
// Try this: | |
// | |
// const delay int64 = 2000000000 // typed const | |
// | |
// Don't forget to comment the `delay := ...` above | |
// EXPLANATION OF THE ERROR: | |
// | |
// Now, `delay` variable and `delay typed const` have a usual int64 type, | |
// not an ideal-numeric type. So, it will create another error. Because, | |
// `time.Sleep` expects a `Duration` type. | |
// We can use the below code to fix the error: | |
// const delay time.Duration = 2000000000 | |
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
// # SUMMARY: | |
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
// As you can see from the lessons above, somestimes it's better to | |
// use untyped constants. They're been created for this kind of | |
// flexibility and I think that untyped constants is a genius idea. | |
fmt.Println("sleeping...") | |
time.Sleep(delay) | |
fmt.Println("woke up!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment