Last active
November 30, 2019 19:48
-
-
Save romanitalian/f984dafd41c581bfdcc347dbf22fad4a to your computer and use it in GitHub Desktop.
golang methods - pointers vs value
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 CodeError struct { | |
text string | |
code string | |
} | |
type ICodeError interface { | |
GetCode() string | |
} | |
func (c CodeError) GetCode() string { | |
return c.code | |
} | |
func (c CodeError) Error() string { | |
return c.text | |
} | |
func NewCodeError(text string, code string) error { | |
return CodeError{text, code} | |
} | |
// assertion-and-interface | |
// https://play.golang.org/p/wpE5oYBK_SG | |
func main() { | |
err := NewCodeError("asdasdfasdf", "11") | |
err1, ok := err.(ICodeError) | |
fmt.Printf("err1: %v\n", err1) | |
fmt.Printf("ok: %v\n", ok) | |
code := err1.GetCode() | |
fmt.Printf("code: %v\n", code) | |
} | |
// err1: asdasdfasdf | |
// ok: true | |
// code: 11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment