Created
September 18, 2013 17:35
-
-
Save tenntenn/6612644 to your computer and use it in GitHub Desktop.
Go言語におけるエイリアス型を使ったパターン ref: http://qiita.com/tenntenn/items/c3afc87a20d9f50998bb
This file contains hidden or 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
package main | |
import "fmt" | |
type Int int | |
func main() { | |
n := 100 | |
m := Int(n) // キャスト | |
fmt.Printf("%T, %T", n, m) | |
} |
This file contains hidden or 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
package main | |
import "fmt" | |
type Hex int | |
func (h Hex) String() string { | |
return fmt.Sprintf("%#x", int(h)) | |
} | |
func main() { | |
fmt.Println(Hex(100)) | |
} |
This file contains hidden or 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
package main | |
import "fmt" | |
type Meter int | |
type Kilogram int | |
type Person struct { | |
Name string | |
Height Meter | |
Weight Kilogram | |
} | |
type Female *Person | |
type Male *Person | |
func femaleOnly(f Female) { | |
fmt.Println(f.Name, "is a female.") | |
} | |
func main() { | |
yamada := &Person{"Yamada", Meter(172), Kilogram(60)} | |
fmt.Println(yamada) | |
femaleOnly(Female(yamada)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment