Are the variables passed to "html/template" passed by reference, by value, or by the original type of the variable?

I understand that maps in Go are passed by reference, but what happens if a 1GB map is a variable to execute a template with “html/template”?

Will another 1GB copy be made in memory before executing it in the template? or will it be passed by reference?

but what happens if a 1GB map is a variable to execute a template with “html/template”?

I don’t quite understand this sentence. But, I assume what you meant is you pass the map as the map to Execute function. In that case, it will be passed by reference, no copy needed.

Yes, I was referring to the Execute function. Thanks for the information.

Here are a few interesting articles:

So technically, the idea of a reference might be incorrect but nonetheless, no copy of the underlying data is made.

3 Likes

Good articles there. Yes, there is no reference in Golang. What we thought as pass-by-reference is actually pass-by-value, but the value is pointer. This is true for other languages like javascript, java, python, etc. I only know reference in C++ and Rust.

1 Like

I got a little bit confused, please just tell me one thing, if I run a template with a 1GB map no other copy of the map will be made in RAM, right?

There is no copy made by passing the map to Execute. However, depending on your template and the Writer that you pass to Execute, you may end up with a serialized version of your map in memory. See Go Playground - The Go Programming Language

Thanks for those links, you made my day :slight_smile: