title: Efficient appending to a variable-length container of strings (Golang)
body: <p>The problem:</p>
  
  <p>I need to apply multiple regexes to each line of a big log file (like several GB long) , gather non-empty matches and put them all in an array (for serialization and sending it over the network).</p>
  
  <p>Slices are not much help if answer to <a href="https://stackoverflow.com/questions/17332227/big-o-of-append-in-golang">this question</a> holds:</p>
  
  <blockquote>
    <p>If the slice does not have sufficient capacity, append will need to allocate new memory and copy the old one over. For slices with &lt;1024 elements, it will double the capacity, for slices with >1024 elements it will increase it by factor 1.25.</p>
  </blockquote>
  
  <p>Since there can be literally hundreds of thousands of regex matches, I can't really predict the length / capacity of a slice. I can't make it too big either "just in case" bc this will waste memory (or will it? if memory allocator is smart enough not to allocate too much memory that is not written into, maybe I could use a huge slice capacity without much harm?).</p>
  
  <p>So I'm thinking about following alternative:</p>
  
  <ol>
  <li>have a doubly-linked list of matches (<a href="http://golang.org/pkg/container/list/" rel="nofollow noreferrer">http://golang.org/pkg/container/list/</a>) </li>
  <li>calc its length (will <code>len()</code> work?)</li>
  <li>preallocate a slice of this capacity</li>
  <li>copy string pointers to this slice</li>
  </ol>
  
  <p>Is there a less laborious way of achieving this goal in Go (append with ~ O(1) append complexity)?</p>
  
  <p>(golang newbie here of course)</p>