Browse Source

Add remaining code of chapter 04

master
T. Meissner 9 months ago
parent
commit
e597f7fea6
6 changed files with 263 additions and 0 deletions
  1. +41
    -0
      chapter_04/activity_4.06/main.go
  2. +40
    -0
      chapter_04/exercise_4.18/main.go
  3. +65
    -0
      chapter_04/exercise_4.19/main.go
  4. +26
    -0
      chapter_04/exercise_4.20/main.go
  5. +29
    -0
      chapter_04/exercise_4.21/main.go
  6. +62
    -0
      chapter_04/exercise_4.22/main.go

+ 41
- 0
chapter_04/activity_4.06/main.go View File

@ -0,0 +1,41 @@
package main
import "fmt"
func getData() []interface{} {
return []interface{}{
1,
3.14,
"hello",
true,
struct{}{},
}
}
func typeCheck(v interface{}) string {
switch v.(type) {
case string:
return "string"
case bool:
return "bool"
case float32, float64:
return "float"
case int, int32, int64:
return "int"
default:
return "unknown"
}
}
func main() {
data := getData()
for i := 0; i < len(data); i++ {
fmt.Printf("%v is %v\n", data[i], typeCheck(data[i]))
}
}

+ 40
- 0
chapter_04/exercise_4.18/main.go View File

@ -0,0 +1,40 @@
package main
import "fmt"
type point struct {
x int
y int
}
func compare() (bool, bool) {
point1 := struct {
x int
y int
}{
10,
10,
}
point2 := struct {
x int
y int
}{}
point2.x = 10
point2.y = 5
point3 := point{10, 10}
return point1 == point2, point1 == point3
}
func main() {
a, b := compare()
fmt.Println("point1 == point2:", a)
fmt.Println("point1 == point3:", b)
}

+ 65
- 0
chapter_04/exercise_4.19/main.go View File

@ -0,0 +1,65 @@
package main
import "fmt"
type name string
type location struct {
x int
y int
}
type size struct {
width int
height int
}
type dot struct {
name
location
size
}
func getDots() []dot {
var dot1 dot
dot2 := dot{}
dot2.name = "A"
dot2.x = 5
dot2.y = 6
dot2.width = 10
dot2.height = 20
dot3 := dot{
name: "B",
location: location{
x: 13,
y: 27,
},
size: size{
width: 5,
height: 7,
},
}
dot4 := dot{}
dot4.name = "C"
dot4.x = 101
dot4.location.y = 209
dot4.width = 87
dot4.size.height = 43
return []dot{dot1, dot2, dot3, dot4}
}
func main() {
dots := getDots()
for i := 0; i < len(dots); i++ {
fmt.Printf("dot%v: %#v\n", i+1, dots[i])
}
}

+ 26
- 0
chapter_04/exercise_4.20/main.go View File

@ -0,0 +1,26 @@
package main
import (
"fmt"
"math"
)
func convert() string {
var i8 int8 = math.MaxInt8
i := 128
f64 := 3.14
m := fmt.Sprintf("int8 = %v -> int64 = %v\n", i8, int64(i8))
m += fmt.Sprintf("int = %v -> int8 = %v\n", i, int8(i))
m += fmt.Sprintf("int8 = %v -> float32 = %v\n", i8, float64(i8))
m += fmt.Sprintf("float64 = %v -> int = %v\n", f64, int(f64))
return m
}
func main() {
fmt.Print(convert())
}

+ 29
- 0
chapter_04/exercise_4.21/main.go View File

@ -0,0 +1,29 @@
package main
import (
"errors"
"fmt"
)
func doubler(v interface{}) (string, error) {
if i, ok := v.(int); ok {
return fmt.Sprint(i * 2), nil
}
if s, ok := v.(string); ok {
return s + s, nil
}
return "", errors.New("unsupported type passed")
}
func main() {
res, _ := doubler(5)
fmt.Println("5 :", res)
res, _ = doubler("yum")
fmt.Println("yum:", res)
_, err := doubler(true)
fmt.Println("true:", err)
}

+ 62
- 0
chapter_04/exercise_4.22/main.go View File

@ -0,0 +1,62 @@
package main
import (
"errors"
"fmt"
)
func doubler(v interface{}) (string, error) {
switch t := v.(type) {
case string:
return t + t, nil
case bool:
if t {
return "truetrue", nil
}
return "falsefalse", nil
case float32, float64:
if f, ok := t.(float64); ok {
return fmt.Sprint(f * 2), nil
}
return fmt.Sprint(t.(float32) * 2), nil
case int:
return fmt.Sprint(t * 2), nil
case int8:
return fmt.Sprint(t * 2), nil
case int16:
return fmt.Sprint(t * 2), nil
case int32:
return fmt.Sprint(t * 2), nil
case int64:
return fmt.Sprint(t * 2), nil
case uint:
return fmt.Sprint(t * 2), nil
case uint8:
return fmt.Sprint(t * 2), nil
case uint16:
return fmt.Sprint(t * 2), nil
case uint32:
return fmt.Sprint(t * 2), nil
case uint64:
return fmt.Sprint(t * 2), nil
default:
return "", errors.New("unsupported type passed")
}
}
func main() {
res, _ := doubler(-5)
fmt.Println("-5: ", res)
res, _ = doubler(5)
fmt.Println("5: ", res)
res, _ = doubler("yum")
fmt.Println("yum: ", res)
res, _ = doubler(true)
fmt.Println("true:", res)
res, _ = doubler(float32(3.14))
fmt.Println("3.14:", res)
}

Loading…
Cancel
Save