Yes. Generating large portions of code via templates is going to be hard. 
Especially reusing and maintaining it.
There is an excellent library called jennifer - 
https://github.com/dave/jennifer that lets you generate Go code by building 
the syntax tree. The examples make it super simple to get started. You 
might want to check it out.

On Monday, September 20, 2021 at 12:29:51 PM UTC-7 va...@selfip.ru wrote:

> I'm writing a code generator from protobuf via templates and i can say - 
> debugging it is very hard. When you have big files you can't check syntax 
> easily, don't know how it looks and if you have errors in the template it 
> is really hard to fix them.
> So the protoc-gen-go case is preferable.
>
> вс, 19 сент. 2021 г. в 04:33, Denis Cheremisov <denis.ch...@gmail.com>:
>
>> Templates is the worst approach to code generation IMO. Take a look how 
>> they do this in protoc-gen-go: 
>>
>> https://github.com/protocolbuffers/protobuf-go/blob/b92717ecb630d4a4824b372bf98c729d87311a4d/cmd/protoc-gen-go/internal_gengo/main.go#L83
>>
>> I am using very similar approach, albeit I prefer format lines, it looks 
>> like:
>> [image: Screenshot from 2021-09-19 04-22-26.png]
>>
>> Templates may be OK only in trivial cases. Once you need something less 
>> trivial it is getting harder and harder to reason how the final code will 
>> look like with them
>> and you will end up with bunch of hard to manage templates. Unlike it, 
>> line-by-line code generation keep staying close to the final code.
>> вторник, 7 сентября 2021 г. в 22:53:51 UTC+3, amitl...@gmail.com: 
>>
>>>
>>> Hi gophers,
>>> I wrote https://github.com/fluhus/goat for generating go code in my 
>>> projects. I hope it can help you too. It's a minimal tool that takes a 
>>> text/template <https://pkg.go.dev/text/template> template as input, 
>>> runs it on the given parameters and gofmt's the output. You can also use it 
>>> on non-go-source.
>>> Feedback is welcome.
>>>
>>> Amit
>>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to golang-nuts...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/406179d9-c8f5-497d-8832-ea04ff9d03b3n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/406179d9-c8f5-497d-8832-ea04ff9d03b3n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>
>
> -- 
> Vasiliy Tolstov,
> e-mail: v.to...@selfip.ru
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/978ebadf-81a6-4375-841b-ee6e15b537c5n%40googlegroups.com.

Reply via email to