-
-
Notifications
You must be signed in to change notification settings - Fork 677
Fill in documentation for type-assertions
#1706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "blurb": "TODO: add blurb", | ||
| "authors": ["ErikSchierboom"], | ||
| "blurb": "Use type assertions to access an interface value's underlying concrete value.", | ||
| "authors": ["jmrunkle"], | ||
| "contributors": [] | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,51 @@ | ||
| # About | ||
| # About Type Assertions | ||
|
|
||
| TODO: add information on type-assertion concept | ||
| Interfaces in Go can introduce ambiguity about the underlying type. | ||
| In fact, the empty interface can take any concrete value at all (including primitives). | ||
| A type assertion allows us to extract the interface value's underlying concrete value using this syntax: `interfaceVariable.(concreteType)`. | ||
|
|
||
| If we assign the result of this statement to a single value, we assert that the interface variable holds a value of the concrete type. | ||
| As a result, it may panic at runtime if the interface value does not have the specified concrete type. | ||
| For example: | ||
|
|
||
| ```go | ||
| var input interface{} = 12 | ||
| str := input.(string) // panic at runtime since input is not a string! | ||
| ``` | ||
|
|
||
| We can test whether an interface value holds a specific concrete type by making use of both return values of the type assertion: the underlying value and a boolean value that reports whether the assertion succeeded. | ||
| For example: | ||
|
|
||
| ```go | ||
| str, ok := input.(string) // no panic if input is not a string | ||
| ``` | ||
|
|
||
| If `input` holds a `string`, then `str` will be the underlying value and `ok` will be true. | ||
| If `input` does not hold a `string`, then `str` will be the zero value of type `string` (ie. `""` - the empty string) and `ok` will be false. | ||
| No panic occurs in any case. | ||
|
|
||
| It is common to see this sort of idiom: | ||
|
|
||
| ```go | ||
| str, ok := input.(string) | ||
| if !ok { | ||
| str = "a default value" | ||
| } | ||
| ``` | ||
|
|
||
| ## Type Switches | ||
|
|
||
| A **type switch** can perform several type assertions in a row. | ||
| It has the same syntax as a type assertion (`interfaceVariable.(concreteType)`), but instead of a specific `concreteType` it uses the keyword `type`. | ||
| Here is an example: | ||
|
|
||
| ```go | ||
| switch v := i.(type) { | ||
| case int: | ||
| fmt.Println("the integer %d", v) | ||
| case string: | ||
| fmt.Println("the string %s", v) | ||
| default: | ||
| fmt.Println("some type we did not handle explicitly") | ||
| } | ||
| ``` | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,41 @@ | ||
| # Introduction | ||
| # Introduction to Type Assertions | ||
|
|
||
| TODO: add introduction for type-assertion concept | ||
| Interfaces in Go can introduce ambiguity about the underlying type. | ||
| A type assertion allows us to extract the interface value's underlying concrete value using this syntax: `interfaceVariable.(concreteType)`. | ||
|
|
||
| For example: | ||
|
|
||
| ```go | ||
| var input interface{} = 12 | ||
| number := input.(int) | ||
| ``` | ||
|
|
||
| NOTE: this will cause a panic if the interface variable does not hold a value of the concrete type. | ||
|
|
||
| We can test whether an interface value holds a specific concrete type by making use of both return values of the type assertion: the underlying value and a boolean value that reports whether the assertion succeeded. | ||
| For example: | ||
|
|
||
| ```go | ||
| str, ok := input.(string) // no panic if input is not a string | ||
| ``` | ||
|
|
||
| If `input` holds a `string`, then `str` will be the underlying value and `ok` will be true. | ||
| If `input` does not hold a `string`, then `str` will be the zero value of type `string` (ie. `""` - the empty string) and `ok` will be false. | ||
| No panic occurs in any case. | ||
|
|
||
| ## Type Switches | ||
|
|
||
| A **type switch** can perform several type assertions in series. | ||
| It has the same syntax as a type assertion (`interfaceVariable.(concreteType)`), but the `concreteType` is replaced with the keyword `type`. | ||
| Here is an example: | ||
|
|
||
| ```go | ||
| switch v := i.(type) { | ||
| case int: | ||
| fmt.Println("the integer %d", v) | ||
| case string: | ||
| fmt.Println("the string %s", v) | ||
| default: | ||
| fmt.Println("some type we did not handle explicitly") | ||
| } | ||
| ``` |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,14 @@ | ||
| [] | ||
| [ | ||
| { | ||
| "url": "https://tour.golang.org/methods/15", | ||
| "description": "A Tour of Go: Type Assertions" | ||
| }, | ||
| { | ||
| "url": "https://tour.golang.org/methods/16", | ||
| "description": "A Tour of Go: Type Switches" | ||
| }, | ||
| { | ||
| "url": "https://golangdocs.com/type-assertions-in-golang", | ||
| "description": "GoDocs: Type Assertions in GoLang" | ||
| } | ||
| ] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.