github.com/timestee/gotemplate@v0.0.0-20170505123500-7c22f8f4ddd0/README.md (about) 1 Go templates 2 ============ 3 4 This tool manages package based templates for the Go language using 5 "go generate" which requires go 1.4. 6 7 [![Build Status](https://travis-ci.org/ncw/gotemplate.png)](https://travis-ci.org/ncw/gotemplate) 8 9 Install 10 ------- 11 12 Install using go get 13 14 go get github.com/ncw/gotemplate/... 15 16 and this will build the `gotemplate` binary in `$GOPATH/bin`. 17 18 It will also pull in a set of templates you can start using straight away 19 20 * [set](http://godoc.org/github.com/ncw/gotemplate/set) 21 * [list](http://godoc.org/github.com/ncw/gotemplate/list) 22 * [sort](http://godoc.org/github.com/ncw/gotemplate/sort) 23 * [heap](http://godoc.org/github.com/ncw/gotemplate/heap) 24 25 Using templates 26 --------------- 27 28 To use a template, first you must tell `gotemplate` that you want to 29 use it using a special comment in your code. For example 30 31 //go:generate gotemplate "github.com/ncw/gotemplate/set" mySet(string) 32 33 This tells `go generate` to run `gotemplate` and that you want to use 34 the set template with the `string` type parameter and with the local 35 name `mySet`. 36 37 Now run `go generate` in your code directory with no arguments. This 38 will instantiate the template into a file called `gotemplate_mySet.go` 39 which will provide a `mySet` type and `newMySet` and `newSizedMySet` 40 functions to make them. Note that the first letter of your custom name 41 is still capitalized when it is not at the beginning of the new name. 42 43 $ go generate 44 substituting "github.com/ncw/gotemplate/set" with mySet(string) into package main 45 Written 'gotemplate_mySet.go' 46 47 If you wish to change what the output file names look like then you 48 can use the `-outfmt format` flag. The format must contain a single 49 instance of the `%v` verb which will be replaced with the template 50 instance name (default "gotemplate_%v") 51 52 Instantiating the templates into your project gives them the ability 53 to use internal types from your project. 54 55 If you use an initial capital when you name your template 56 instantiation then any external functions will be public. Eg 57 58 //go:generate gotemplate "github.com/ncw/gotemplate/set" MySet(string) 59 60 Would give you `MySet`, `NewMySet` and `NewSizedMySet` instead. 61 62 You can use multiple templates and you can use the same template with 63 different parameters. In that case you must give it a different name, 64 eg 65 66 //go:generate gotemplate "github.com/ncw/gotemplate/set" StringSet(string) 67 //go:generate gotemplate "github.com/ncw/gotemplate/set" FloatSet(float64) 68 69 If the parameters have spaces in then they need to be in quotes, eg 70 71 //go:generate gotemplate "github.com/ncw/gotemplate/sort" "SortGt(string, func(a, b string) bool { return a > b })" 72 73 Renaming rules 74 -------------- 75 76 All top level identifiers will be substituted when the template is 77 instantiated. This is to ensure that they are unique if the template 78 is instantiated more than once. 79 80 Any identifiers with the template name in (eg `Set`) will have the 81 template name (eg `Set`) part substituted. If the template name does 82 not begin the identifier, Go's casing style is respected and the 83 first letter of your new identifier is capitalized. (eg 'newMySet' 84 instead of 'newmySet'). 85 86 Any identifiers without the template name in will just be post-fixed 87 with the template name. 88 89 So if this was run 90 91 //go:generate gotemplate "github.com/ncw/gotemplate/set" MySet(string) 92 93 This would substitute these top level identifiers 94 95 * `Set` to `MySet` 96 * `NewSet` to `NewMySet` 97 * `NewSizedSet` to `NewSizedMySet` 98 * `utilityFunc` to `utilityFuncMySet` 99 100 Depending on whether the template name is public (initial capital) or 101 not, all the public external identifiers will have their initial 102 capitals turned into lower case. So if this was run 103 104 //go:generate gotemplate "github.com/ncw/gotemplate/set" mySet(string) 105 106 This would substitute 107 108 * `Set` to `mySet` 109 * `NewSet` to `newMySet` 110 * `NewSizedSet` to `newSizedMySet` 111 * `utilityFunc` to `utilityFuncMySet` 112 113 Installing templates 114 -------------------- 115 116 Templates can be installed using `go get` because they are normal Go 117 packages. Eg 118 119 go get github.com/someones/template 120 121 Will install a template package you can use in your code with 122 123 //go:generate gotemplate "github.com/someones/template" T(Potato) 124 125 Then instantiate with 126 127 go generate 128 129 Source control for templates 130 ---------------------------- 131 132 It is expected that the generated files will be checked into version 133 control, and users of your code will just run `go get` to fetch it. 134 `go generate` will only be run by developers of the package. 135 136 Writing templates 137 ----------------- 138 139 Templates are valid go packages. They should compile and have tests 140 and be usable as-is. Because they are packages, if you aren't writing 141 a public template you should put them in a subdirectory of your 142 project most likely. 143 144 To make a Go package a template it should have one or more 145 declarations and a special comment signaling to `gotemplate` what the 146 template is called and what its parameters are. Supported 147 parameterized declarations are type, const, var and func. 148 149 Here is an example from the set package. 150 151 // template type Set(A) 152 type A int 153 154 This indicates that the base name for the template is `Set` and it has 155 one type parameter `A`. When you are writing the template package 156 make sure you use `A` instead of `int` where you want it to be 157 substituted with a new type when the template is instantiated. 158 159 Similarly, you could write a package with a const parameter. 160 161 // template type Vector(A, N) 162 type A int 163 const N = 2 164 165 type Vector[N]A 166 167 This indicates that the base name for the template is `Vector` and it 168 has one type parameter `A` and one constant parameter `N`. Again, all 169 uses of `N` in the template code will be replaced by a literal value 170 when the template is instantiated. 171 172 All the definitions of the template parameters will be removed from 173 the instantiated template. 174 175 All test files are ignored. 176 177 Bugs 178 ---- 179 180 There may be constraints on the types which aren't understood by 181 `gotemplate`. For instance the set requires that the types are 182 comparable. If you try this you'll get a compile error for example. 183 184 //go:generate gotemplate "github.com/ncw/gotemplate/set" BytesSet([]byte) 185 186 Only one .go file is used when reading template definitions at the 187 moment (programmer laziness - will fix at some point!) 188 189 Changelog 190 --------- 191 192 * v0.06 - 2017-05-05 193 * Add -outfmt string (thanks Paul Jolly) 194 * v0.05 - 2016-02-26 195 * Fix docs and examples 196 * More set methods - thanks Adam Willis 197 * Fix missing error check in code generation 198 * v0.04 - 2014-12-23 199 * Fixed multi-line type declarations 200 * v0.03 - 2014-12-22 201 * Allow const and var to be substituted as template parameters 202 * v0.02 - 2014-12-15 203 * Fixed multi-line const/var declarations 204 * v0.01 - 2014-12-10 205 * Change renaming rules to make better Go names. This only affects private exports, eg for `mySet` in the example above, 206 * `NewSet` becomes `newMySet` (was `newmySet`) 207 * `NewSizedSet` becomes `newSizedMySet` (was `newSizedmySet`) 208 * `utilityFunc` becomes `utilityFuncMySet` (`utilityFuncmySet`) 209 * This is a backwards incompatible change 210 * v0.00 - 2014-10-05 211 * First public release 212 213 Ideas for the future 214 -------------------- 215 216 Make a set type for non comparable things? Pass in a compare routine? 217 218 Make sure that types implement an interface? 219 220 Optional parameters? 221 222 Philosophy 223 ---------- 224 225 All code (templates, use of templates and template instantiations) 226 should be normal Go code - no special types / extensions. 227 228 All configuration done with specially formatted comments 229 230 Should provide lots practical templates people can use right now. 231 232 License 233 ------- 234 235 This is free software under the terms of MIT the license (check the 236 COPYING file included in this package). 237 238 Portions of the code have been copied from the Go source. These are 239 identified by comments at the head of each file and these are 240 Copyright (c) The Go Authors. See the GO-LICENSE file for full details. 241 242 Contact and support 243 ------------------- 244 245 The project website is at: 246 247 * https://github.com/ncw/gotemplate 248 249 There you can file bug reports, ask for help or contribute patches. 250 251 Authors 252 ------- 253 254 * Nick Craig-Wood <nick@craig-wood.com> 255 256 Contributors 257 ------------ 258 259 * Patrick Oyarzun <patrickoyarzun@gmail.com> 260 * Adam Willis <akwillis@inbox.com> 261 * Paul Jolly <paul@myitcv.org.uk>