Just to add a tidbit to what Jan said. The key here is that strings (type string) in Go are immutable, whereas strings ("char *" based types) in C are not. That is why the same string can be used again and again without ever needing to be copied, and why they can live in the text segment.
On Tuesday, November 1, 2022 at 3:47:47 PM UTC-4 Jan Mercl wrote: > > > On Tue, Nov 1, 2022, 20:36 Frank Jüdes <jue...@gmail.com> wrote: > >> I have to write a program that should verify the content of >> configuration-servers. It will need a lot of pre-initialized data to verify >> the actual content of a server, so it has to initialize many strings. What >> i know from other C-style languages is, that code like >> >> var MyString *string = 'Hello World!'; >> >> Will result in having two identical copies of the string »Hello World!« >> in the memory: The first one within the program-code itself and a second >> copy somewhere in the heap-memory. >> > > I think the string backing array will be in the text segment as in C. The > string header will end in the data segment, provided it's a package scoped > variable, but the header has no direct equivalent in C. > > How will the go-compiler handle something like this: >> >> package main >> import ("fmt") >> type MyStruct struct { >> Text string >> Count int32 >> } >> func main() { >> MyVar := MyStruct{Text: "Hello World!", Count: 20 } >> fmt.Printf("%#v\n",MyVar) } >> >> Will there be two copies of the string »Hello World!" in the memory or >> just one? >> > > The backing string array will exist only once, again in the text segment, > I believe, because there's no reason for making any copies of it in this > case. > >> > Not tested/verified > >> >> -- 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/c44f9c26-f4fe-4e5b-9c33-562b6b490ab8n%40googlegroups.com.