Hello Zachary
Stdin and os.Args are quite different.
If your wrote a go program named "kaplan", then when running the following 
command on a unix-like system (linux, or OSX) :

  cat bigphoto.jpg | kaplan sepia downscale > result.jpg

you have os.Args[0]=="kaplan",  os.Args[1]=="sepia", 
 os.Args[2]=="downscale".  They are all strings indeed, but it is unlikely 
that using them will cause any inefficiency, because the total length of 
the command-line arguments is usually small (<1000).

you also have all the bytes from the contents of the file "bigphoto.jpg" 
streamed to the os.Stdin of your program.
os.Stdin is a os.File, which implements io.Reader.  It is designed to 
handle bytes, not strings.  More precisely, you would call os.Stdin.Read to 
put the bytes into the []byte of your choice.

If your func :
- wants to return a short, human-readable text: return a string.
- wants to build a long and immutable human-redable text: make a buf 
[]byte, fill it, then return string(buf).
- wants to return binary data: return a []byte.
- wants to return text data that the caller will modify: return a []byte.

Also, you may find interesting (for efficiency) that reslicing a substring 
doesn't create a copy :   
  t := s[30:55] 
t shares the same immutable array in memory as s.  See video Lexical 
Scanning in Go <https://www.youtube.com/watch?v=HxaD_trXwRE> : the whole 
lexer may parse the input file as one big string, without ever copying the 
tokens characters : tokens are just "slices" of the source code.

HTH,
Val


On Wednesday, May 10, 2017 at 7:15:01 PM UTC+2, Zachary Kaplan wrote:
>
> in golang, strings are immutable. thus, if i want to modify a string in 
> place, i'll need to cast or copy each element from the string to a similar 
> but mutable data structure. this seems inefficient for simple command line 
> programs that take input from stdin and access it through os.Args. can 
> somebody please explain to me when a function should to return a string as 
> opposed to, say a []byte? thanks
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to