Skip to content

Instantly share code, notes, and snippets.

@keisatou
Created January 8, 2019 11:52
Show Gist options
  • Save keisatou/eba239aed065d8f3a7b9ca18290ee0f3 to your computer and use it in GitHub Desktop.
Save keisatou/eba239aed065d8f3a7b9ca18290ee0f3 to your computer and use it in GitHub Desktop.
Stringer() vs GoStringer()
package main
import "fmt"
type AccessCheckStatus int
const (
_ AccessCheckStatus = iota
// Allow ...
Allow
// Deny ...
Deny
// DenyNotMatched ...
DenyNotMatched
// DenyByPolicy ...
DenyByPolicy
// DenyRoleNotFound ...
DenyRoleNotFound
// DenyDomainNotMatched ...
DenyDomainNotMatched
// DenyInvalidSignature ...
DenyInvalidSignature
// DenySignatureNotFound ...
DenySignatureNotFound
// DenyInvalidExpire ...
DenyInvalidExpire
// DenyExpired ...
DenyExpired
)
// activate the following comment-outed code
// to see %#v prints string.
// func (ac AccessCheckStatus) GoString() string {
// return ac.String()
// }
func (ac AccessCheckStatus) String() string {
switch ac {
case Allow:
return "Allow"
case Deny:
return "Deny"
case DenyNotMatched:
return "DenyNotMatched"
case DenyByPolicy:
return "DenyByPolicy"
case DenyRoleNotFound:
return "DenyRoleNotFound"
case DenyDomainNotMatched:
return "DenyDomainNotMatched"
case DenyInvalidSignature:
return "DenyInvalidSignature"
case DenySignatureNotFound:
return "DenySignatureNotFound"
case DenyInvalidExpire:
return "DenyInvalidExpire"
case DenyExpired:
return "DenyExpired"
default:
return "Unknown"
}
}
func main() {
hoge(Allow)
}
// https://golang.org/pkg/fmt/#GoStringer
func hoge(value interface{}) {
fmt.Println("entering hoge")
// %v the value in a default format
// when printing structs, the plus flag (%+v) adds field names
fmt.Printf("%%v: %v\n", value)
// %#v a Go-syntax representation of the value
fmt.Printf("%%#v: %#v\n", value)
// %T a Go-syntax representation of the type of the value
fmt.Printf("%%T: %T\n", value)
fmt.Println("leaving hoge")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment