lab golang study tutorials 02
Thu Jul 22, 2021Last modified: Thu Jul 22, 2021
-
Categories: (directories)
- π Computer Engineering
-
Series: (travel the world of knowledge!)
- π lab golang study
Go tutorials
- ππλ³Έ λ΄μ©μ tour.golang.org μ μλ λ΄μ©μ μ 리ν κΈμ λλ€.
- method λΆν° concurrency μ κΉμ§
Go method!
package main
import (
"fmt"
"math"
)
type Vertex struct {
X, Y float64
}
func (v Vertex) AbsMethod() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
func AbsFunc(v Vertex) float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
type MyFloat float64
// λ©μλ λ§λ€λ μ£Όμμ¬ν, μΌλ° νμ
μλκ³ custom νμ
λ§ κ°λ₯ν¨. κ°μ ν¨ν€μ§ λ΄μ μ μΈλμ΄ μμ΄μΌν¨. ~~(μ«μ΄νλ μ΄μ κ° κ³μ λμ΄λλ€!)~~
func (f MyFloat) Abs() float64 {
if f < 0 {
return float64(-f)
}
return float64(f)
}
func (v *Vertex) AbsPointer(n float64) float64 {
v.X = v.X * n
v.Y = v.Y * n
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
func (v Vertex) Scale(f float64) {
v.X = v.X * f
v.Y = v.Y * f
}
func main() {
v := Vertex{3, 4}
fmt.Println(v.Abs())
fmt.Println(Abs(v))
f := MyFloat(-math.Sqrt2)
fmt.Println(f.Abs())
v := Vertex{3, 4}
fmt.Println(v.AbsPointer(2))
// ν¬μΈν°λ₯Ό μμ°λ©΄ κ°κ° λ€λ₯Έ κ°μ²΄(볡μ¬λ κ° μ¬μ©)
// ν¬μΈν°λ₯Ό μ°λ©΄ κ°μ λ νΌλ°μ€λ₯Ό μ°Έμ‘°νλκΉ κ°μ κ°μ²΄
v.Scale(10)
fmt.Println(v.AbsPointer(2))
}
λ ΈνΈ
-
λ©μλ μ£Όμμ¬ν
-
ν¨μλ μΈμ λ£μλ ν¬μΈν° ꡬλΆν¨(&λΆμ¬μΌλ¨) λ©μλλ μΈμ λ£μλ ν¬μΈν° μμμ νλ¨ν¨ μ§μ§ μ°Έ μΌκ΄μ μΌλ‘ μνΈν μΈμ΄…
μΈμ΄λ μμ΄ νΈνλ° κ°λ°μκ° λΆνΈ…v.Scale(5) == (&v).Scale(5)λ€νν μ°Έμ‘°μ μμ°Έμ‘° λλ€ κ°μ νΉμ§μ κ°μ§ p.Abs() λΌλ λ©μλλ (*p).Abs() λ‘λ μ¬μ©κ°λ₯ κ·ΈλκΉ value receiverμ΄λ reference receiver μ΄λ λ©μλλ μ무 μ κ²½μμ λμ λ°λμ μ£Όμν μ μ, λ νΌλ°μ€ 리μλ²λ κ°μ λ°κΎΈκ³ , κ° λ¦¬μλ²λ 볡μ¬λ³Έμ λ§λ¬
μΌλ°μ μΌλ‘ νΉμ μ νμ λͺ¨λ λ°©λ²μλ κ°μ΄λ ν¬μΈν° 리μλ²κ° μμ΄μΌ νμ§λ§ λ λ€ νΌν©λμ΄μλ μλ©λλ€.
type Abser interface { Abs() float64 } func main() { var a Abser f := MyFloat(-math.Sqrt2) v := Vertex{3, 4} a = f // a MyFloat implements Abser fmt.Println(a.Abs()) //1.4142135623730951 a = &v // a *Vertex implements Abser // In the following line, v is a Vertex (not *Vertex) // and does NOT implement Abser. // a = v fmt.Println(a.Abs()) // 5 } type MyFloat float64 func (f MyFloat) Abs() float64 { if f < 0 { return float64(-f) } return float64(f) } type Vertex struct { X, Y float64 } func (v *Vertex) Abs() float64 { return math.Sqrt(v.X*v.X + v.Y*v.Y) }μ¬κΈ°μ λ μ λ§€ν μ¬μ€μ,
func (v Vertex) Abs() float64 { return math.Sqrt(v.X*v.X + v.Y*v.Y) } func (v *Vertex) Abs() float64 { return math.Sqrt(v.X*v.X + v.Y*v.Y) }μ΄λ κ² νλ©΄ redeclared λΌκ³ νλ©΄μ, μκΉ μμ μ μν©μμλ
func main() { a = &v // a *Vertex implements Abser fmt.Println(a.Abs()) // In the following line, v is a Vertex (not *Vertex) // and does NOT implement Abser. a = v fmt.Println(a.Abs()) } func (v Vertex) Abs() float64 { return math.Sqrt(v.X*v.X + v.Y*v.Y) }μ΄κ±΄ λ μ μ€ν λλ€… κ° λ¦¬μλ²μμλ μ£Όμ λ£μ΄λ λκ³ , κ° λ£μ΄λ λλλ°, μ£Όμ 리μλ²μμλ κΌ μ£Όμλ§ λ£μ΄μΌ λλ€. μ΄κ² λμΌ?
-
-
μλ°λ₯Ό μλͺ°λΌμ… λ¬΄μ¨ λ»μ΄μ§?
- μΈν°νμ΄μ€μ μμμ ꡬν
- type implementsλ λ©μλλ₯Ό μ€νν¨μΌλ‘μ¨ μΈν°νμ΄μ€λ₯Ό ꡬνν©λλ€.
λͺ μμ intentμ μ μΈλ, “implementation"μ ν€μλλ μμ΅λλ€.
μμμ μΈν°νμ΄μ€λ μΈν°νμ΄μ€μ μ μλ₯Ό ꡬνμΌλ‘λΆν° λΆλ¦¬νλ©°,
μ΄λ μ¬μ μ λ ¬ μμ΄ μ΄λ ν ν¨ν€μ§μ λ±μ₯ν μ μμ΅λλ€.
?
- type implementsλ λ©μλλ₯Ό μ€νν¨μΌλ‘μ¨ μΈν°νμ΄μ€λ₯Ό ꡬνν©λλ€.
- μΈν°νμ΄μ€μ μμμ ꡬν
-
μλ λμ¨ main μ ν¨ν€μ§ μ΄λ¦μΌλ―? ({Hello}, main.T) Hello (3.141592653589793, main.F)
-
μμ΄κ³΅λΆ
- Under the hood : λ΄λΆ
- concrete type? ꡬ체μ μΈ νμ μ΄λ¦μ λ§νλ κ±°κ² μ§? μΆμ νμ λ μλ? auto κ°μκ±°? ννμ΄λΌκ³ νλκΉ (value, type) μ§κ΄μ μΌλ‘λ λ¬΄μ¨ λ§μ νλ €κ³ νλμ§ μλλ μκ² λλ°, λ§μ λͺ¨λ₯΄κ² λ€. μλ¬΄νΌ μ΄λ¦ κ°μλ νμ λ€λ₯΄λ©΄ λ€λ₯Έκ±°λΌλ κ±°μ§.
-
nil receiver κ° μ’λ€κ³ νλλ°
type I interface { M() } type T struct { S string } func (t *T) M() { // if t == nil { // fmt.Println("this is",t) // return // } fmt.Println(t) } /* μ£Όμ μ²λ¦¬ νλ©΄ (<nil>, *main.T) <nil> (&{hello}, *main.T) &{hello} μ£Όμ νλ©΄ (<nil>, *main.T) this is <nil> (&{hello}, *main.T) &{hello} t λ₯Ό *t λ‘ νλ©΄ t.S μ κ°μ μλ¬ λ°μ panic: runtime error: invalid memory address or nil pointer dereference λ λ©λλ‘ ν΄λΌ... μλλ μ€ν μλ¨. func (t *T) M() { if t == nil { fmt.Println("this is",t) return } fmt.Println(*t) } */ func main() { var i I var t *T i = t describe(i) i.M() i = &T{"hello"} describe(i) i.M() } func describe(i I) { fmt.Printf("(%v, %T)\n", i, i) } -
type assertion
var i interface{} = "hello" s := i.(string) fmt.Println(s) s, ok := i.(string) fmt.Println(s, ok) f, ok := i.(float64) fmt.Println(f, ok) f = i.(float64) // panic fmt.Println(f) -
type switch
func do(i interface{}) { switch v := i.(type) { case int: fmt.Printf("Twice %v(%T) is %v\n", v,v, v*2) case string: fmt.Printf("%q(%T) is %v bytes long\n", v,v, len(v)) default: fmt.Printf("I don't know about type %T!\n", v) } } func main() { do(21) do("hello") do(true) } -
Stringers ν¨μλ₯Ό λ§λ€μ΄λμΌλ©΄ printν λ μ€μ νκ² μ²λΌ λμ΄ ν¨μ μ£Όμμ²λ¦¬νλ©΄ κ·Έλ₯ κ°μ²΄λ§ λμ΄(μλ μ€μ ν λ¬Έμ₯μ΄ μλλΌ)
type Person struct { Name string Age int } func (p Person) String() string { return fmt.Sprintf("%v (%v years)", p.Name, p.Age) } func main() { a := Person{"Arthur Dent", 42} z := Person{"Zaphod Beeblebrox", 9001} fmt.Println(a, z) }
io.Reader μ νμ
? μλ§ λ€λ₯Έ μΈν°νμ΄μ€ (*strings.Reader)
error, io.EOF μ νμ
μ (*errors.errorString)
// μ§λλ stringsκΉμ§ νμ
image κΉμ§ νκΈ΄ νλλ°,
image λΌμ΄λΈλ¬λ¦¬λ λμ€μ μ¬μ©νκ² λλ©΄ λ€μ 보λκ² λμλ―