0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

go修行3日目 no-struct

Posted at

no-struct


package main

import "fmt"

type MyInt int

func (i MyInt) Double() int{
	// integerにキャストする
	return int(i * 2)
}

func main() {
	myInt := MyInt(10)
	fmt.Println(myInt.Double())
}
20

インターフェース


package main

import "fmt"

type Human interface {
	Say() string
}

type Person struct{
	Name string
}

func (p *Person) Say() string{
	p.Name = "Mr." + p.Name
	fmt.Println(p.Name)
	return p.Name
}

// Humanインターフェースを受け付ける
func DriveCar(human Human){
	if human.Say() == "Mr.Mike" {
		fmt.Println("Run")
	}else{
		fmt.Println("Get Out")
	}
}

func main(){
	var mike Human = &Person{"Mike"}
	var x Human = &Person{"X"}
	DriveCar(mike)
	DriveCar(x)
}
Mr.Mike
Run
Mr.X
Get Out
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?