Skip to content

Instantly share code, notes, and snippets.

@Mo3g4u
Created August 20, 2022 07:14
Show Gist options
  • Save Mo3g4u/16e65773d8ffdaa8d1a2247226eb4a44 to your computer and use it in GitHub Desktop.
Save Mo3g4u/16e65773d8ffdaa8d1a2247226eb4a44 to your computer and use it in GitHub Desktop.
機密情報を扱うフィールドを定義して出力書式をカスタマイズする
package main
import (
"encoding/json"
"fmt"
)
type ConfidentialCustomer struct {
CustomerID int64
CreditCard CreditCard
}
type CreditCard string
func (c CreditCard) String() string {
return "xxxx-xxxx-xxxx-xxxx"
}
func (c CreditCard) GoString() string {
return "xxxx-xxxx-xxxx-xxxx"
}
func main() {
c := ConfidentialCustomer{
CustomerID: 1,
CreditCard: "4111-1111-1111-1111",
}
fmt.Println(c)
fmt.Printf("%v\n", c)
fmt.Printf("%+v\n", c)
fmt.Printf("%#v\n", c)
bytes, _ := json.Marshal(c)
fmt.Println("JSON: ", string(bytes))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment