github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/docs/how-to-guides/simple-library.md (about)

     1  ---
     2  id: simple-library
     3  ---
     4  
     5  # How to write a simple Gno Library (Package)
     6  
     7  ## Overview
     8  
     9  This guide shows you how to write a simple library (Package) in Gno, which can be used by other Packages and Realms.
    10  Packages are _stateless_, meaning they do not hold state like regular Realms (Smart Contracts). To learn more about the
    11  intricacies of Packages, please see the [Packages concept page](../concepts/packages.md).
    12  
    13  The Package we will be writing today will be a simple library for suggesting a random tapas dish.
    14  We will define a set list of tapas, and define a method that randomly selects a dish from the list.
    15  
    16  ## Development environment
    17  Currently, Gno packages can be developed locally or via the online editor, Gno
    18  Playground. Below we detail how to set up and use both.
    19  
    20  ### Local setup
    21  
    22  #### Prerequisites
    23  
    24  - **Text editor**
    25  
    26  :::info Editor support
    27  The Gno language is based on Go, but it does not have all the bells and whistles in major text editors like Go.
    28  Advanced language features like IntelliSense are still in the works.
    29  
    30  Currently, we officially have language support
    31  for [ViM](https://github.com/gnolang/gno/blob/master/CONTRIBUTING.md#vim-support),
    32  [Emacs](https://github.com/gnolang/gno/blob/master/CONTRIBUTING.md#emacs-support)
    33  and [Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=harry-hov.gno).
    34  :::
    35  
    36  We discussed Gno folder structures more in detail in
    37  the [simple Smart Contract guide](simple-contract.md#1-setting-up-the-work-directory).
    38  For now, we will just follow some rules outlined there.
    39  
    40  Create the main working directory for our Package:
    41  
    42  ```bash
    43  mkdir tapas-lib
    44  ```
    45  
    46  Since we are building a simple tapas Package, inside our created `tapas-lib` directory, we can create another
    47  directory named `p`, which stands for `package`:
    48  
    49  ```bash
    50  cd tapas-lib
    51  mkdir p
    52  ```
    53  
    54  Additionally, we will create another subdirectory that will house our Package code, named `tapas`:
    55  
    56  ```bash
    57  cd p
    58  mkdir tapas
    59  ```
    60  
    61  After setting up our work directory structure, we should have something like this:
    62  
    63  ```text
    64  tapas-lib/
    65  ├─ p/
    66  │  ├─ tapas/
    67  │  │  ├─ // source code here
    68  ```
    69  
    70  Now that the work directory structure is set up, we can go into the `tapas` sub-folder, and actually create
    71  our tapas suggestion library logic:
    72  
    73  ```bash
    74  cd tapas
    75  touch tapas.gno
    76  ```
    77  
    78  You're ready to write Gno code! Skip to ["Start writing code"](#2-start-writing-code)
    79  to see how to start.
    80  
    81  ### Using Gno Playground
    82  
    83  When using the Gno Playground, writing, testing, deploying, and sharing Gno code
    84  is simple. This makes it perfect for getting started with Gno.
    85  
    86  Visiting the [Playground](https://play.gno.land) will greet you with a template file:
    87  
    88  ![Default](../assets/how-to-guides/simple-library/playground_welcome.png)
    89  
    90  Create a new file named `tapas.gno`, and delete the default file. You are now
    91  ready to write some Gno code!
    92  
    93  
    94  ## 2. Start writing code
    95  
    96  After setting up your environment, we can start defining our library logic. 
    97  Inside `tapas.gno`:
    98  
    99  [embedmd]:# (../assets/how-to-guides/simple-library/tapas.gno go)
   100  ```go
   101  package tapas
   102  
   103  import "std"
   104  
   105  // List of tapas suggestions
   106  var listOfTapas = []string{
   107  	"Patatas Bravas",
   108  	"Gambas al Ajillo",
   109  	"Croquetas",
   110  	"Tortilla Española",
   111  	"Pimientos de Padrón",
   112  	"Jamon Serrano",
   113  	"Boquerones en Vinagre",
   114  	"Calamares a la Romana",
   115  	"Pulpo a la Gallega",
   116  	"Tostada con Tomate",
   117  	"Mejillones en Escabeche",
   118  	"Chorizo a la Sidra",
   119  	"Cazón en Adobo",
   120  	"Banderillas",
   121  	"Espárragos a la Parrilla",
   122  	"Huevos Rellenos",
   123  	"Tuna Empanada",
   124  	"Sardinas a la Plancha",
   125  }
   126  
   127  // GetTapaSuggestion randomly selects and returns a tapa suggestion
   128  func GetTapaSuggestion(userInput string) string {
   129  
   130  	// Create a pseudorandom number depending on the block height.
   131  	// We get the block height using std.GetHeight(), which is from an imported Gno library, "std"
   132  	// Note: this value is not fully random and is easily guessable
   133  	randomNumber := int(std.GetHeight()) % len(listOfTapas)
   134  
   135  	// Return the random suggestion
   136  	return listOfTapas[randomNumber]
   137  }
   138  ```
   139  
   140  There are a few things happening here, so let's dissect them:
   141  
   142  - We defined the logic of our library into a package called `tapas`.
   143  - The package imports `std`, which
   144  is the [Gno standard library](../concepts/stdlibs/stdlibs.md)
   145  - We use the imported package inside of `GetTapaSuggestion` to generate a
   146  random index value for a tapa
   147  
   148  You can view the code on [this Playground link](https://play.gno.land/p/3uwBqP66ekC).
   149  
   150  ## Conclusion
   151  
   152  That's it 🎉
   153  
   154  You have successfully built a simple tapas suggestion Package that is ready to be deployed on the Gno chain and imported
   155  by other Packages and Realms.