main.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. //go:generate gotext update -out catalog.go
  6. import (
  7. "golang.org/x/text/language"
  8. "golang.org/x/text/message"
  9. )
  10. func main() {
  11. p := message.NewPrinter(language.English)
  12. p.Print("Hello world!\n")
  13. p.Println("Hello", "world!")
  14. person := "Sheila"
  15. place := "Zürich"
  16. p.Print("Hello ", person, " in ", place, "!\n")
  17. // Greet everyone.
  18. p.Printf("Hello world!\n")
  19. city := "Amsterdam"
  20. // Greet a city.
  21. p.Printf("Hello %s!\n", city)
  22. town := "Amsterdam"
  23. // Greet a town.
  24. p.Printf("Hello %s!\n",
  25. town, // Town
  26. )
  27. // Person visiting a place.
  28. p.Printf("%s is visiting %s!\n",
  29. person, // The person of matter.
  30. place, // Place the person is visiting.
  31. )
  32. pp := struct {
  33. Person string // The person of matter. // TODO: get this comment.
  34. Place string
  35. extra int
  36. }{
  37. person, place, 4,
  38. }
  39. // extract will drop this comment in favor of the one below.
  40. // argument is added as a placeholder.
  41. p.Printf("%[1]s is visiting %[3]s!\n", // Person visiting a place.
  42. pp.Person,
  43. pp.extra,
  44. pp.Place, // Place the person is visiting.
  45. )
  46. // Numeric literal
  47. p.Printf("%d files remaining!", 2)
  48. const n = 2
  49. // Numeric var
  50. p.Printf("%d more files remaining!", n)
  51. // Infer better names from type names.
  52. type referralCode int
  53. const c = referralCode(5)
  54. p.Printf("Use the following code for your discount: %d\n", c)
  55. // Using a constant for a message will cause the constant name to be
  56. // added as an identifier, allowing for stable message identifiers.
  57. // Explain that a device is out of order.
  58. const msgOutOfOrder = "%s is out of order!" // FOO
  59. const device = "Soda machine"
  60. p.Printf(msgOutOfOrder, device)
  61. // Double arguments.
  62. miles := 1.2345
  63. p.Printf("%.2[1]f miles traveled (%[1]f)", miles)
  64. }