issue121:c_c
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue121:c_c [2017/06/09 08:28] – d52fr | issue121:c_c [2017/06/11 14:07] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | **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. | + | **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 | + | J'ai commencé récemment à travailler sur un projet utilisant le langage de programmation Go de Google. Comme je ne l' |
+ | |||
+ | **The Setup | ||
You can install Go using apt: | You can install Go using apt: | ||
Ligne 8: | Ligne 10: | ||
If you want a more recent version of Go, there are more instructions on their GitHub page: https:// | If you want a more recent version of Go, there are more instructions on their GitHub page: https:// | ||
+ | |||
+ | Le paramétrage | ||
+ | |||
+ | Vous pouvez installer Go en utilisant apt : | ||
+ | |||
+ | sudo apt-get install golang-go | ||
+ | |||
+ | Si vous voulez une version plus récente de Go, il y a des instructions supplémentaires sur leur page GitHub : https:// | ||
**Example Code | **Example Code | ||
Ligne 14: | Ligne 24: | ||
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.** | 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.** | ||
+ | |||
+ | Exemple de code | ||
+ | |||
+ | J'ai regroupé un petit jeu d' | ||
+ | |||
+ | Si vous décidez de télécharger les fichiers, veillez à placer les deux fichiers html dans un sous-répertoire appelé « tmpl » ou mettez à jour le chemin dans le fichier app.go. | ||
**The Basics | **The Basics | ||
Ligne 20: | Ligne 36: | ||
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.** | 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.** | ||
+ | |||
+ | Les bases | ||
+ | |||
+ | Il y a une présentation sur https:// | ||
+ | |||
+ | Une fois que vous avez écrit votre code, vous pouvez, soit le compiler en utilisant « go build », soit le lancer localement en utilisant « go run ». Même si Go peut être utilisé pour des projets autres que les applis basées sur le Web, je ne vais pas rentrer dans le détail pour ces utilisations. | ||
**Templating | **Templating | ||
Ligne 28: | Ligne 50: | ||
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).** | 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).** | ||
+ | |||
+ | Les modèles | ||
+ | |||
+ | le code exemple ci-dessus est principalement une application « hello world », mais recouvrant quelques aspects que j'ai eu du mal à implémenter correctement d' | ||
+ | • À la ligne 23 (https:// | ||
+ | • Sur la même ligne, la ligne ParseGlob est nécessaire pour travailler avec des modèles partiels, car, sans cela, la ligne {{define “header”}} ne sera pas analysée, conduisant à des erreurs. Si vous n' | ||
+ | |||
+ | De manière générale, cette préparation de modèles fonctionne de manière proche de Jinja2, pour tous ceux qui l'ont utilisé. Les éléments dynamiques (tels que ceux chargés à partir de « structs ») sont entre accolades, et tous les champs commencent par un « . », indiquant que vous vous attendez à trouver la variable dans l' | ||
**URLs | **URLs | ||
Ligne 38: | Ligne 68: | ||
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”).** | 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”).** | ||
+ | |||
+ | Les URL | ||
+ | |||
+ | Si vous lancez « go run » dans le répertoire du projet, une navigation à http:// | ||
+ | |||
+ | L' | ||
+ | |||
+ | Le code lui-même est relativement simple - à la ligne 15, je paramètre la page générique et, ensuite, je vérifie que la longueur du chemin du L'URL (tout ce qui est après le domaine) est plus grande que la longueur de « / » (soit 1). Si cette longueur est plus grande, cela signifie qu'un paramètre est transmis (dans ce cas, le nom) et la variable « hello » est écrasée par le nouveau titre/ | ||
+ | |||
+ | Après la déclaration » if », le reste de la fonction viewHandler est dicté par le paquet de modèles. funcMap contient un « map » ( pour toute personne qui utilise Python, ça ressemble aux dictionnaires) de noms de filtres et les fonctions vers lesquelles ils dirigent. Ils peuvent être aussi utilisés dans des fonctions personnalisées. Après cela, les fichiers de modèles sont chargés. La fonction « template.Must » assure simplement que le programme lance une erreur et une panique si aucun fichier de modèle n'est trouvé. Enfin, la fonction ExecuteTemplate réclame un ResponseWriter (« w »), le nom du modèle à charger (sur la base de son nom de fichier) et l' | ||
**Structs | **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.** | 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.** | ||
+ | |||
+ | Structs | ||
+ | |||
+ | Les lignes 9-12 créent un struct nommé « Page » qui contient deux champs de chaînes de caractères : Title (titre) et Content (contenu). Principalement, | ||
**Debugging | **Debugging | ||
If you run into issues, you can import the “fmt” package, and use fmt.Printf to print formatted text to the terminal.** | If you run into issues, you can import the “fmt” package, and use fmt.Printf to print formatted text to the terminal.** | ||
+ | |||
+ | Débogage | ||
+ | |||
+ | Si, en fonctionnement, | ||
**Conclusion | **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.** | 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.** | ||
+ | |||
+ | Conclusion | ||
+ | |||
+ | Ceci est un exemple très basique, mais il devrait servir de point d' | ||
issue121/c_c.1496989687.txt.gz · Dernière modification : 2017/06/09 08:28 de d52fr