指针
函数返回值
场景1:函数的返回值是子结构体
函数的返回值是基础结构体,使用新结构体的指针形式实现基础结构体,若函数返回新结构体那么只能返回新结构的指针
package mainimport "fmt"type CustomError struct {s string}func (e *CustomError)Error() string {return e.s}func New(mes string) error {customError := CustomError{ s: mes }return &customError // 返回结构体的指针}func main() {e := New("Some thing happen")fmt.Println(e)}
场景2: 类型断言
子类型使用指针实现基础类型
package mainimport "fmt"// HttpError structtype HttpError struct {message stringmethod stringstatus int}func (httpError *HttpError) Error() string{return fmt.Sprintf("The method func %v, method status %v, method message %v", httpError.method, httpError.status, httpError.message)}func getEmail(userID string) (string, error) {return "", &HttpError{message: "Something error",method: "Get",status: 403,}}func main() {_, err := getEmail("12")fmt.Println(err)if err != nil {errVal := err.(*HttpError) //断言至基础类型的指针形式fmt.Println(errVal.status)}}
场景3:接口动态类型断言
无论是接口方法接收类型是指针还是普通都可以使用 i.(Type) 获取接口的动态结构体
package mainimport "fmt"// UserSessionState ...type UserSessionState interface {errorisLoggedIn() boolgetSessionId() int}// UnauthorizedError ...type UnauthorizedError struct{UserID intSessionID int}func (err *UnauthorizedError)isLoggedIn() bool {return err.SessionID != 0}func (err *UnauthorizedError)getSessionId() int{return err.SessionID}func (err *UnauthorizedError)Error() string{return fmt.Sprintf("User with id %v cannot perform current ation", err.SessionID)}func validateUser(userID int) error {return &UnauthorizedError{userID, 12312}}func main() {err := validateUser(123)if err != nil {fmt.Println(err)if errVal, ok := err.(UserSessionState); ok { // 获取接口的动态类型fmt.Println("Will cleaning session id ", errVal.getSessionId())errNestedVal, ok := err.(*UnauthorizedError)if (ok) {fmt.Println("Nested val", errNestedVal.UserID)}}} else {fmt.Println("User is allowed to perform this action")}}
断言判断
从channel中读数据
val, ok := <- c
结构体断言
errVal, ok := err.(*HttpError)或者errVal, ok := err.(HttpError)
