First In First Out (FIFO) example


Golf has built-in FIFO list type. You can create a FIFO variable and then store key/value string pairs in it, which you can then read back in the order you put them in. You can also rewind the FIFO list and obtain the same key/value pairs over and over again if needed.

This is an example of FIFO list usage. This small command-line application will store key/value pairs "key1"/"value1" and "key2"/"value2" and then obtain them twice.

First, let's create a directory for our application:
mkdir test-list
cd test-list/

And then create the application itself:
gg -k list-app

Copy and paste the Golf code below to file "list.golf":
 begin-handler /list
     silent-header

     // Create a list
     new-fifo mylist

     // Add data to the list
     write-fifo mylist key "key1" value "value1"
     write-fifo mylist key "key2" value "value2"

     start-loop
      // Get data from the list
      read-fifo mylist key k value v status st
      // Check if no more data
      if-true st not-equal GG_OKAY
          break-loop
      end-if
      @Obtained key <<print-out k>> with value <<print-out v>>
     end-loop

     // Go through the list again, use rewind-fifo for that
     rewind-fifo mylist
     start-loop
         read-fifo mylist key k value v status st
         if-true st not-equal GG_OKAY
             break-loop
         end-if
         @Again obtained key <<print-out k>> with value <<print-out v>>
     end-loop

     // Delete FIFO list
     purge-fifo mylist
 end-handler

The code above uses new-fifo statement to create a FIFO variable, write-fifo to add key/value pairs to it, and read-fifo to obtain them back in the order they were put in. You can use rewind-fifo to rewind back to the beginning at any time, as many times as you need.

Make the executable:
gg -q --public

Execute the program:
gg -r --req="/list" --app="/list-app" --exec

The result is as expected:
Obtained key key1 with value value1
Obtained key key2 with value value2
Again obtained key key1 with value value1
Again obtained key key2 with value value2

See also
Articles
article-capi  
article-cookies  
article-debug  
article-distributed  
article-encryption  
article-fetch-web-page  
article-fifo  
article-file-manager  
article-hello-server  
article-hello-world  
article-hello-world-service  
article-hello-world-service-web  
article-how-to-create-golf-application  
article-json  
article-language  
article-mariadb  
article-memory-safety  
article-memory-safety-web  
article-notes-postgres  
article-random  
article-regex  
article-remote-call  
article-request-function  
article-security  
article-sendmail  
article-server  
article-shopping  
article-sqlite  
article-statements  
article-status-check  
article-tree  
article-tree-web  
article-vim-coloring  
article-web-framework-for-c-programming-language  
article-what-is-golf  
article-what-is-web-service  
See all
documentation


Copyright (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.