-
Notifications
You must be signed in to change notification settings - Fork 1
e Workshop 03 : Invoke a Go function, without GopherSauce abstractions as pipeline
In this workshop you will learn how to write a function, without a Gopher Sauce abstraction and use it as a template pipeline.
- Go +v1.7 with
$GOPATHset. - GopherSauce ( to manage module). Learn here.
- ~ 15 minutes time.
-
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/ -
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 -
Change the working directory to
gospkgto begin developing the module.
-
Run the following command to setup a new project within the working directory :
gos --make -
Open the file
gos.gxml, within your project's directory. -
Update the tag deploy from
<deploy>webapp</deploy>to<deploy>package</deploy>. -
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.
(With your gospkg directory as the working directory.)
-
Within your working directory, add a new file named
gos.go. -
Add the following code to file
gos.go. This will declare a struct method for generated typePKG. TypePKGwill be used to export your function to your templates as a pipeline :package gospkg import "fmt" func (pkg PKG) Greet(name string) string { return fmt.Sprintf("Hi, %s", name) }-
Let's make sure to write a unit test for this function. Add a new file named
gos_test.goto your project with the following content :package gospkg import "testing" import "fmt" func TestFetch(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) } }
-
-
Run the following command to export your package. This will enable use of modules with other projects.
gos --export
-
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 -
Create a new Gopher Sauce project with the following command :
gos --make
-
Open the file
gos.gxmlwithin your project's directory, with your favorite code editor. -
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"/>
-
Create a new file within your project's
webdirectory. This file will be namedindex.tmpl. -
Open file
web/index.tmplwith your favorite text editor, and add the following line to import this new module.{{ $pkg := gospkg }} -
Add the following line to
web/index.tmplto invoke the module function, and greet the user :{{ $pkg.Greet "Go User"}}
-
Run the following command to launch the server :
gos --run -
Open your favorite browser to localhost:8080 to view the result.