Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions concepts/type-assertion/.meta/config.json
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": []
}
52 changes: 50 additions & 2 deletions concepts/type-assertion/about.md
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")
}
```
42 changes: 40 additions & 2 deletions concepts/type-assertion/introduction.md
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")
}
```
15 changes: 14 additions & 1 deletion concepts/type-assertion/links.json
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"
}
]