Outils pour utilisateurs

Outils du site


issue121:c_c

Ceci est une ancienne révision du document !


I recently started working on a project using Google’s Go programming language. As I had previously only completed the tour and a few minor tasks, this was my first experience using it for a larger project. Over the course of the project, I am essentially creating a web app (linked to a PostgreSQL database). I won’t go into the details of the actual project, but I will be sharing what I’ve learned so far.

The Setup

You can install Go using apt:

sudo apt-get install golang-go

If you want a more recent version of Go, there are more instructions on their GitHub page: https://github.com/golang/go/wiki/Ubuntu

Example Code

I’ve thrown together a small set of sample code into a Gist, which can be found here: https://gist.github.com/lswest/feed0fb8685b0d9bed03e864a78f7f1a

If you decide to download the files, be sure to place the two html files in a subdirectory called “tmpl”, or update the path in the app.go file.

The Basics

There is a tour over at https://tour.golang.org/welcome/1 which is a very good spot to start. Naturally, you can also follow other tutorials or books.

Once you’ve written your code, you can either compile it using go build or run it locally using go run. While Go can be used for other projects besides web-based apps, I won’t be going into detail for those uses.

Templating

The above example code is essentially a ‘hello world’ application, but covering a few aspects that I had trouble implementing correctly right off the bat. A few important notes: • On line 23 (https://gist.github.com/lswest/feed0fb8685b0d9bed03e864a78f7f1a#file-app-go-L23) the Funcs(funcMap) section adds in customized filters for use in the template (in this case, enabling the use of a ToLower filter). • On the same line, the ParseGlob line is necessary for working with partial templates, as, without it, the define_header line would not be parsed, leading to errors. If you’re not using a tmpl directory, ParseGlob(“*”) should work.

Generally, this templating works pretty much like Jinja2, for anyone who has used that. Dynamic elements (such as those loaded from structs) are in curly braces, and all fields begin with a “.”, indicating that you’re expecting to find the variable in the current object. Filters are attached to variables via a pipe. Stringing multiple filters together should work (though I haven’t tested it).

URLs

If you execute ‘go run’ in the project directory, navigating to http://localhost:8081/ will yield a generic looking “Hello World” file. If, however, you head to http://localhost:8081/Lucas, the page will instead greet the name given in the URL.

The example is very basic - if you want some “homework,” try to (for example) correctly capitalize the name, even if it’s lowercase in the URL.

The code itself is relatively straightforward - on line 15, I set up the generic page, and then I check if the length of the URL Path (everything after the domain) is longer than the length of “/” (so, 1). If the length is longer, that means there is a parameter given (in this case, the name), and the ‘hello’ variable gets overwritten with the new title/content. If it’s shorter or equal to 1, then it should just be the fallback page, so nothing changes (as there is no ‘else’ statement).

After the ‘if’ statement, the rest of the viewHandler function is dictated by the templates package. The funcMap contains a map (for anyone who uses Python, they can be thought of as dictionaries) of filter names and the functions they map to. This could be used for custom functions as well. Afterwards, the template files are loaded. The function ‘template.Must’ simply ensures that the program throws an error and a panic if no template files are found. And lastly, the ExecuteTemplate function asks for a ResponseWriter (“w”), the name of the template to load (based off the filename), and the object to load (“hello”).

Structs

Lines 9-12 create a struct called “Page” which contains two string fields - Title and Content. This is essentially creating a special data type. Structs can be initialized via Page{Title, Content}. Naturally, the more variables you include, the longer the initialization is.

Debugging

If you run into issues, you can import the “fmt” package, and use fmt.Printf to print formatted text to the terminal.

Conclusion

This is a very basic example, but should still serve as a jumping in point for anyone interested in working with Go. If anyone runs into issues, or has suggestions how to complete any of the steps in a more “Go Fashion”, feel free to let me know at lswest34+fcm@gmail.com. Similarly, if you have any requests for article topics I should cover, you can let me know via email.

issue121/c_c.1496069554.txt.gz · Dernière modification : 2017/05/29 16:52 de auntiee