golang.org/x/text@v0.14.0/cmd/gotext/examples/extract/main.go (about) 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 5 package main 6 7 //go:generate gotext update -out catalog.go 8 9 import ( 10 "golang.org/x/text/language" 11 "golang.org/x/text/message" 12 ) 13 14 func main() { 15 p := message.NewPrinter(language.English) 16 17 p.Print("Hello world!\n") 18 19 p.Println("Hello", "world!") 20 21 person := "Sheila" 22 place := "Zürich" 23 24 p.Print("Hello ", person, " in ", place, "!\n") 25 26 // Greet everyone. 27 p.Printf("Hello world!\n") 28 29 city := "Amsterdam" 30 // Greet a city. 31 p.Printf("Hello %s!\n", city) 32 33 town := "Amsterdam" 34 // Greet a town. 35 p.Printf("Hello %s!\n", 36 town, // Town 37 ) 38 39 // Person visiting a place. 40 p.Printf("%s is visiting %s!\n", 41 person, // The person of matter. 42 place, // Place the person is visiting. 43 ) 44 45 pp := struct { 46 Person string // The person of matter. // TODO: get this comment. 47 Place string 48 extra int 49 }{ 50 person, place, 4, 51 } 52 53 // extract will drop this comment in favor of the one below. 54 // argument is added as a placeholder. 55 p.Printf("%[1]s is visiting %[3]s!\n", // Person visiting a place. 56 pp.Person, 57 pp.extra, 58 pp.Place, // Place the person is visiting. 59 ) 60 61 // Numeric literal 62 p.Printf("%d files remaining!", 2) 63 64 const n = 2 65 66 // Numeric var 67 p.Printf("%d more files remaining!", n) 68 69 // Infer better names from type names. 70 type referralCode int 71 72 const c = referralCode(5) 73 p.Printf("Use the following code for your discount: %d\n", c) 74 75 // Using a constant for a message will cause the constant name to be 76 // added as an identifier, allowing for stable message identifiers. 77 78 // Explain that a device is out of order. 79 const msgOutOfOrder = "%s is out of order!" // FOO 80 const device = "Soda machine" 81 p.Printf(msgOutOfOrder, device) 82 83 // Double arguments. 84 miles := 1.2345 85 p.Printf("%.2[1]f miles traveled (%[1]f)", miles) 86 }