Skip to content

e Workshop 02 : Invoke a function from module.

Cheikh Seck edited this page Nov 2, 2018 · 2 revisions

e-Workshop 02 : Invoke a function from module.

Overview

In this workshop you will learn how to write a function and use it within a template module.

Checklist

  • Go +v1.7 with $GOPATH set.
  • GopherSauce. Learn here.
  • ~ 15 minutes time.

Setup

  1. To get started, open your terminal of choice, then run the following command (this is to go to the root of your $GOPATH):

     cd $GOPATH/src/
    
  2. Run the next command to make two new directories, one will be used as a package, and the other, as a web server.

     mkdir gospkg && mkdir gostest
    
  3. Change the working directory to gospkg to begin developing the module.

Start the project

  1. Run the following command to setup a new project within the working directory :

     gos --make
    
  2. Open the file gos.gxml, within your project's directory.

  3. Update the tag deploy from <deploy>webapp</deploy> to <deploy>package</deploy>.

  4. Update the tag package from <package>if-package-is-library</package> to <package>gospkg</package>, the new value set will be the name of the template pipeline used to load module.

Create a module function

(With your gospkg directory as the working directory.)

  1. Open the file gos.gxml within your project's directory, with your favorite code editor.

  2. Within your gos.gxml file, add the following function tag within your existing <methods/> tag. The function will be used to generate a new greeting, it has one parameter to indicate the name of the person being greeted. The tag should be as follows :

    <func name="Greet" var="name string" return="string">
    	return fmt.Sprintf("Hi, %s", name)
    </func>
    
    1. Let's make sure to write a unit test for this function. Add a new file named gos_test.go to your project with the following content :

       package gospkg
      
       import "testing"
       import "fmt"
       
       func TestGreet(t *testing.T) {
       
       	// name of test case
       	name := "test"
       
       	// Desired greeting
       	greeting := fmt.Sprintf("Hi, %s", name)
       
       	// load package to test function with
       	pkg := PKG{}
       
       	// invoke function as struct method,
       	// pass expected parameter as separate
       	// string
       	result := pkg.Greet("test")
       
       	if result != greeting {
       		t.Errorf("Greeting was incorrect, got: %s, want: %s.", result, greeting)
       	}
       }
      

Export package

  1. Run the following command to export your package. This will enable use of modules with other projects.

     gos --export
    

Switch to web server.

  1. Run the following command to switch to the web server project. We will use the module we just created with an active web page.

     cd ../gostest 
    
  2. Create a new Gopher Sauce project with the following command :

     gos --make
    

Import module to project.

  1. Open the file gos.gxml within your project's directory, with your favorite code editor.

  2. Import the module we just created with the following import tag, add it after your existing </error> tag. The path is relative to your $GOPATH

     <import src="gospkg/gos.gxml"/>
    

Use function within module

  1. Create a new file within your project's web directory. This file will be named index.tmpl.

  2. Open file web/index.tmpl with your favorite text editor, and add the following line to import this new module.

     {{ $pkg := gospkg }}
    
  3. Add the following line to web/index.tmpl to invoke the module function, and greet the user :

     {{ $pkg.Greet "Go User"}}
    

Run and test

  1. Run the following command to launch the server :

     gos --run
    
  2. Open your favorite browser to localhost:8080 to view the result.