-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09aabb5
commit 416811e
Showing
4 changed files
with
105 additions
and
7 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package simple | ||
|
||
import "fmt" | ||
|
||
func Generics() { | ||
slice1 := []int{5, 3, 7, 1} | ||
fmt.Println(slice1) | ||
fmt.Println(*reverse(slice1)) | ||
|
||
fmt.Println() | ||
|
||
slice2 := []string{"b", "g", "a", "m"} | ||
fmt.Println(slice2) | ||
fmt.Println(*reverse(slice2)) | ||
|
||
} | ||
|
||
//Creating custom constraints can make your code easier to read/understand. | ||
type customConstraint interface { | ||
int | string | ||
} | ||
|
||
//Generics are useful when you want to reuse code dealing with different argument/return types. | ||
//T is a generic type (in this case, either an int or a string). | ||
func reverse[T customConstraint](slice []T) *[]T { | ||
reverseSlice := make([]T, len(slice)) | ||
|
||
j := 0 | ||
for i := len(slice) - 1; i >= 0; i-- { | ||
reverseSlice[j] = slice[i] | ||
j++ | ||
} | ||
|
||
return &reverseSlice | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package simple | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
) | ||
|
||
// Go does not require the usage of "implements" or similar terms - the implementation is implicit. | ||
type Geometry interface { | ||
Area() float64 | ||
} | ||
|
||
func DisplayGeometryArea(g Geometry) { | ||
fmt.Println("Area:", g.Area()) | ||
} | ||
|
||
type Rectangle struct { | ||
height, width float64 | ||
} | ||
|
||
type Circle struct { | ||
radius float64 | ||
} | ||
|
||
func (r *Rectangle) Area() float64 { | ||
return r.width * r.height | ||
} | ||
|
||
func (c *Circle) Area() float64 { | ||
return math.Pi * math.Pow(c.radius, 2) | ||
} | ||
|
||
func Interfaces() { | ||
r := Rectangle{ | ||
height: 2, | ||
width: 4, | ||
} | ||
|
||
c := Circle{ | ||
radius: 3, | ||
} | ||
|
||
DisplayGeometryArea(&r) | ||
DisplayGeometryArea(&c) | ||
|
||
fmt.Println() | ||
|
||
//The empty interface can have different types of value | ||
var slice []interface{} | ||
|
||
slice = append(slice, 10) | ||
slice = append(slice, 8.4) | ||
slice = append(slice, true) | ||
slice = append(slice, "test") | ||
|
||
fmt.Println(slice) | ||
|
||
for index, value := range slice { | ||
if val, ok := value.(string); ok { | ||
fmt.Println(val+" is a string; index =", index) | ||
} else { | ||
fmt.Println("index =", index) | ||
} | ||
} | ||
} |