Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Strings in Go

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


in Java it is a known problem to change strings at runtime especially in loops and very often.
That lead to StringBuilder/Buffer.

How does Go handle that problem or is there any for Go at all?

Given
var s string = "Some text"
var t string = "Some text"
would mean at runtime s == t in Java (afaik)
Also in Go? Because the byte field is the same and the compiler could/should know it...

Harald
 
author
Posts: 37
VI Editor Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harald,

In Go, strings are immutable just as they are in Java and Python. This has many benefits both in terms of performance and for writing robust concurrent programs.

Given
var s string = "Some text"
var t string = "Some text"
would mean at runtime s == t in Java (afaik)



This is an implementation detail that may or may not be true for any given JVM. The same applies in Python. In Go, s == t is true (== sensibly does the string comparison), but &s != &t (i.e., each has a different address so each string is unique). However, the address uniqueness is still just an implementation detail and shouldn't be assumed.

Go's equivalent to Java's StringBuilder is bytes.Buffer:



This assumes you have some readStringFromSomewhere() function that returns an empty string when it is finished. A slightly more sophisticated example is shown in "Programming in Go" on page 88.
 
Seriously? That's what you're going with? I prefer this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!