M
M
MIsternik2017-04-15 16:27:32
go
MIsternik, 2017-04-15 16:27:32

Working with the base and slices, how to do it right?

Recently I became interested in the language and I just can’t understand some points.
1. Slices are references to arrays or mx part in memory, does this mean that it is possible to transfer and return slices from functions by value?
2. SQL has Prepare. As I understand it, it should be used if the request is repeated.
But repetitions are meant in a loop, before which we first do Prepare and then execute ? Or is it possible after sql.Open () when initializing the application to "prepare" queries and then use them during the entire lifetime of the application? For a web application, for example.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2017-04-17
@pav5000

You need to break it into 2 different questions. I answer the first one.
Yes, slices are just pointers, so they can be passed by value.
https://play.golang.org/p/rVehEbb3-n
But only for cases where their length does not change while the function is running. For at allocation of a new piece of storage the pointer will change.
https://play.golang.org/
And even like this: https://play.golang.org/p/-bDHwT2Jcm
PS In the standard sort package, slices are passed by value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question