fork download
  1. package main
  2.  
  3. import (
  4. "bufio"
  5. "fmt"
  6. "os"
  7. "reflect"
  8. "regexp"
  9. "strings"
  10. )
  11.  
  12. type Profile struct {
  13. mine MyProfile "My datas"
  14. yours YourProfile "Your datas"
  15. }
  16.  
  17. type MyProfile struct {
  18. name string
  19. email string
  20. phone string
  21. occupation []string
  22. language []string
  23. hobby []string
  24. }
  25.  
  26. type YourProfile struct {
  27. name string
  28. gender string
  29. }
  30.  
  31. func NewProfile(arr map[string]map[string]interface{}) *Profile {
  32. return &Profile{
  33. mine: SetProfile(arr["me"]), // ★ ここをどうすればいいのかサッパリ分かりません
  34. yours: SetProfile(arr["you"]), // ★
  35. }
  36. }
  37.  
  38. type M interface {
  39. ShowMessage() string
  40. }
  41.  
  42. func (p *Profile) ShowMessage() string {
  43. return fmt.Sprintf("Hi, %s. \nName: %s \nLanguage: %s", p.yours.name, p.mine.name,
  44. strings.Join(p.mine.language, ", "))
  45. }
  46.  
  47. func (p *Profile) SetProfile(arr map[string]interface{}) {
  48. for k, v := range arr {
  49. key := reflect.ValueOf(p).Elem().FieldByName(k)
  50. if key.IsValid() {
  51. key.SetString(v.(string))
  52. }
  53. }
  54. }
  55.  
  56. func (m *MyProfile) SetProfile(arr map[string]interface{}) {
  57. for k, v := range arr {
  58. key := reflect.ValueOf(m).Elem().FieldByName(k)
  59. if key.IsValid() {
  60. key.SetString(v.(string))
  61. }
  62. }
  63. }
  64.  
  65. func (u *YourProfile) SetProfile(arr map[string]string) {
  66. for k, v := range arr {
  67. key := reflect.ValueOf(u).Elem().FieldByName(k)
  68. if key.IsValid() {
  69. key.SetString(v)
  70. }
  71. }
  72. }
  73.  
  74. func ArrangeAnswer(ans string) string {
  75. re := regexp.MustCompile(`(\s| )+`)
  76. return strings.TrimSpace(re.ReplaceAllString(ans, " "))
  77. }
  78.  
  79. func main() {
  80. prof := make(map[string]map[string]interface{})
  81.  
  82. prof["me"] = map[string]interface{}{
  83. "name": "John Lennon",
  84. "email": "foobar@gmail.com",
  85. "phone": "+81-90-0000-0000",
  86. "occupation": []string{"Programmer", "System Engineer"},
  87. "language": []string{"Go", "Java", "Python", "PHP", "JavaScript", "C"},
  88. }
  89.  
  90. scanner := bufio.NewScanner(os.Stdin)
  91. fmt.Println("Would you please let me know your profile?")
  92. for {
  93. fmt.Print("Your name: ")
  94. scanner.Scan()
  95. ans := ArrangeAnswer(scanner.Text())
  96. re := regexp.MustCompile(`^[a-zA-Z ]+$`)
  97. if re.MatchString(ans) {
  98. prof["you"]["name"] = ans
  99. break
  100. } else {
  101. fmt.Println("alphabet please.")
  102. }
  103. }
  104. fmt.Print("Your gender: ")
  105. scanner.Scan()
  106. prof["you"]["gender"] = strings.ToLower(ArrangeAnswer(scanner.Text()))
  107. if err := scanner.Err(); err != nil {
  108. fmt.Fprintln(os.Stderr, "Read error: ", err)
  109. }
  110.  
  111. p := NewProfile(prof)
  112. fmt.Println(p.ShowMessage())
  113. }
  114.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Tom York
male
compilation info
# _/home/MWiOwq
./prog.go:33: undefined: SetProfile
./prog.go:34: undefined: SetProfile
stdout
Standard output is empty