├── README.md ├── db.sql ├── main-pt-br.go ├── main-uncommented.go ├── main.go └── tmpl ├── Edit.tmpl ├── Footer.tmpl ├── Header.tmpl ├── Index.tmpl ├── Menu.tmpl ├── New.tmpl └── Show.tmpl /README.md: -------------------------------------------------------------------------------- 1 | # Golang Mysql CRUD 2 | This is an CRUD(Create, Read, Update, Delete) using MySQL Database and Golang 3 | 4 | There's 3 source files: 5 | * main.go -> Comments in English 6 | * main-pt-br.go -> Comments in Portuguese - Brazil 7 | * main-uncommentd -> Without any comments 8 | 9 | All the code was written in english including the Template files. 10 | 11 | This was build for study :) 12 | -------------------------------------------------------------------------------- /db.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `names` ( 2 | `id` int(6) unsigned NOT NULL AUTO_INCREMENT, 3 | `name` varchar(30) NOT NULL, 4 | `email` varchar(30) NOT NULL, 5 | PRIMARY KEY (`id`) 6 | ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1; 7 | 8 | -------------------------------------------------------------------------------- /main-pt-br.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "database/sql" // Pacote Database SQL para realizar query 5 | "log" // Mostra informações no console 6 | "net/http" // Gerencia URLs e Servidor Web 7 | "text/template" // Gerencia templates 8 | 9 | _ "github.com/go-sql-driver/mysql" // Driver Mysql para Go 10 | ) 11 | 12 | // Struct utilizada para exibir dados no template 13 | // Essa struct deve ter os mesmos campos do banco de dados 14 | type Names struct { 15 | Id int 16 | Name string 17 | Email string 18 | } 19 | 20 | // Função dbConn, abre conexão com banco de dados 21 | func dbConn() (db *sql.DB) { 22 | 23 | dbDriver := "mysql" // Driver do banco de dados 24 | dbUser := "" // Usuário 25 | dbPass := "" // Senha 26 | dbName := "" // Nome do banco 27 | 28 | // Realiza a conexão com banco de dados: 29 | // sql.Open("DRIVER", "Usuario:Senha/@BancoDeDados) 30 | // A variavel `db` é utilizada junto com pacote `database/sql` 31 | // para a montagem de Query. 32 | // A variavel `err` é utilizada no tratamento de erros 33 | db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName) 34 | if err != nil { 35 | panic(err.Error()) 36 | } 37 | return db 38 | } 39 | 40 | // Usa a variável tmpl para renderizar todos os templates da pasta `tmpl` 41 | // independente da extenção 42 | var tmpl = template.Must(template.ParseGlob("tmpl/*")) 43 | 44 | // Função Index, usada para renderizar o arquivo Index 45 | func Index(w http.ResponseWriter, r *http.Request) { 46 | // Abre a conexão com banco de dados utilizando a função dbConn() 47 | db := dbConn() 48 | 49 | // Realiza a consulta com banco de dados e trata erros 50 | selDB, err := db.Query("SELECT * FROM names ORDER BY id DESC") 51 | if err != nil { 52 | panic(err.Error()) 53 | } 54 | 55 | // Monta a struct para ser utilizada no template 56 | n := Names{} 57 | 58 | // Monta um array para guardar os valores da struct 59 | res := []Names{} 60 | 61 | // Realiza a estrutura de repetição pegando todos os valores do banco 62 | for selDB.Next() { 63 | // Armazena os valores em variaveis 64 | var id int 65 | var name, email string 66 | 67 | // Faz o Scan do SELECT 68 | err = selDB.Scan(&id, &name, &email) 69 | if err != nil { 70 | panic(err.Error()) 71 | } 72 | 73 | // Envia os resultados para a struct 74 | n.Id = id 75 | n.Name = name 76 | n.Email = email 77 | 78 | // Junta a Struct com Array 79 | res = append(res, n) 80 | 81 | } 82 | 83 | // Abre a página Index e exibe todos os registrados na tela 84 | tmpl.ExecuteTemplate(w, "Index", res) 85 | 86 | // Fecha conexão 87 | defer db.Close() 88 | } 89 | 90 | // Função Show exibe apenas um resultado 91 | func Show(w http.ResponseWriter, r *http.Request) { 92 | db := dbConn() 93 | 94 | // Pega o ID do parametro da URL 95 | nId := r.URL.Query().Get("id") 96 | 97 | // Usa o ID para fazer a consulta e trata erros 98 | selDB, err := db.Query("SELECT * FROM names WHERE id=?", nId) 99 | if err != nil { 100 | panic(err.Error()) 101 | } 102 | 103 | // Monta a struct para ser utilizada no template 104 | n := Names{} 105 | 106 | // Realiza a estrutura de repetição pegando todos os valores do banco 107 | for selDB.Next() { 108 | // Armazena os valores em variaveis 109 | var id int 110 | var name, email string 111 | 112 | // Faz o Scan do SELECT 113 | err = selDB.Scan(&id, &name, &email) 114 | if err != nil { 115 | panic(err.Error()) 116 | } 117 | 118 | // Envia os resultados para a struct 119 | n.Id = id 120 | n.Name = name 121 | n.Email = email 122 | 123 | } 124 | 125 | // Mostra o template 126 | tmpl.ExecuteTemplate(w, "Show", n) 127 | 128 | // Fecha a conexão 129 | defer db.Close() 130 | 131 | } 132 | 133 | // Função New apenas exibe o formulário para inserir dados 134 | func New(w http.ResponseWriter, r *http.Request) { 135 | tmpl.ExecuteTemplate(w, "New", nil) 136 | } 137 | 138 | // Função Edit, edita uma linha 139 | func Edit(w http.ResponseWriter, r *http.Request) { 140 | // Abre conexão com banco de dados 141 | db := dbConn() 142 | 143 | // Pega o ID do parametro da URL 144 | nId := r.URL.Query().Get("id") 145 | 146 | // Realiza consulta usando o ID e trata erros 147 | selDB, err := db.Query("SELECT * FROM names WHERE id=?", nId) 148 | if err != nil { 149 | panic(err.Error()) 150 | } 151 | 152 | // Monta a struct para ser utilizada no template 153 | n := Names{} 154 | 155 | // Realiza a estrutura de repetição pegando todos os valores do banco 156 | for selDB.Next() { 157 | // Armazena os valores em variaveis 158 | var id int 159 | var name, email string 160 | 161 | // Faz o Scan do SELECT 162 | err = selDB.Scan(&id, &name, &email) 163 | if err != nil { 164 | panic(err.Error()) 165 | } 166 | 167 | // Envia os resultados para a struct 168 | n.Id = id 169 | n.Name = name 170 | n.Email = email 171 | 172 | } 173 | 174 | // Mostra o template com formulário preenchido para edição 175 | tmpl.ExecuteTemplate(w, "Edit", n) 176 | 177 | // Fecha conexão com banco de dados 178 | defer db.Close() 179 | } 180 | 181 | // Função Insert, insere valores no banco de dados 182 | func Insert(w http.ResponseWriter, r *http.Request) { 183 | 184 | // Abre conexão com banco de dados usando a função: dbConn() 185 | db := dbConn() 186 | 187 | // Verifica o METHOD do fomulário passado 188 | if r.Method == "POST" { 189 | 190 | // Pega os campos do formulário 191 | name := r.FormValue("name") 192 | email := r.FormValue("email") 193 | 194 | // Prepara a SQL e verifica errors 195 | insForm, err := db.Prepare("INSERT INTO names(name, email) VALUES(?,?)") 196 | if err != nil { 197 | panic(err.Error()) 198 | } 199 | 200 | // Insere valores do formulário com a SQL tratada e verifica erros 201 | insForm.Exec(name, email) 202 | 203 | // Exibe um log com o valores digitados no formulário 204 | log.Println("INSERT: Name: " + name + " | E-mail: " + email) 205 | } 206 | 207 | // Encerra a conexão do dbConn() 208 | defer db.Close() 209 | 210 | // Retorna a HOME 211 | http.Redirect(w, r, "/", 301) 212 | } 213 | 214 | // Função Insert, insere valores no banco de dados 215 | func Update(w http.ResponseWriter, r *http.Request) { 216 | 217 | // Abre conexão com banco de dados usando a função: dbConn() 218 | db := dbConn() 219 | 220 | // Verifica o METHOD do fomulário passado 221 | if r.Method == "POST" { 222 | 223 | // Pega os campos do formulário 224 | name := r.FormValue("name") 225 | email := r.FormValue("email") 226 | id := r.FormValue("uid") 227 | 228 | // Prepara a SQL e verifica errors 229 | insForm, err := db.Prepare("UPDATE names SET name=?, email=? WHERE id=?") 230 | if err != nil { 231 | panic(err.Error()) 232 | } 233 | 234 | // Insere valores do formulário com a SQL tratada e verifica erros 235 | insForm.Exec(name, email, id) 236 | 237 | // Exibe um log com o valores digitados no formulário 238 | log.Println("UPDATE: Name: " + name + " | E-mail: " + email) 239 | } 240 | 241 | // Encerra a conexão do dbConn() 242 | defer db.Close() 243 | 244 | // Retorna a HOME 245 | http.Redirect(w, r, "/", 301) 246 | } 247 | 248 | // Função Insert, insere valores no banco de dados 249 | func Delete(w http.ResponseWriter, r *http.Request) { 250 | 251 | // Abre conexão com banco de dados usando a função: dbConn() 252 | db := dbConn() 253 | 254 | nId := r.URL.Query().Get("id") 255 | 256 | // Prepara a SQL e verifica errors 257 | delForm, err := db.Prepare("DELETE FROM names WHERE id=?") 258 | if err != nil { 259 | panic(err.Error()) 260 | } 261 | 262 | // Insere valores do formulário com a SQL tratada e verifica erros 263 | delForm.Exec(nId) 264 | 265 | // Exibe um log com o valores digitados no formulário 266 | log.Println("DELETE") 267 | 268 | // Encerra a conexão do dbConn() 269 | defer db.Close() 270 | 271 | // Retorna a HOME 272 | http.Redirect(w, r, "/", 301) 273 | } 274 | 275 | func main() { 276 | 277 | // Exibe mensagem que o servidor iniciou 278 | log.Println("Server started on: http://localhost:9000") 279 | 280 | // Gerencia as URLs 281 | http.HandleFunc("/", Index) // Mostra todos os registros 282 | http.HandleFunc("/show", Show) // Template: Um registro 283 | http.HandleFunc("/new", New) // Template: Novo registro 284 | http.HandleFunc("/edit", Edit) // Template: Edita registro 285 | 286 | // Ações 287 | http.HandleFunc("/insert", Insert) // Ação: Novo Registro 288 | http.HandleFunc("/update", Update) // Ação: Edita registro 289 | http.HandleFunc("/delete", Delete) // Ação: Deletar registro 290 | // Inicia o servidor na porta 9000 291 | http.ListenAndServe(":9000", nil) 292 | 293 | } 294 | -------------------------------------------------------------------------------- /main-uncommented.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "database/sql" 5 | "log" 6 | "net/http" 7 | "text/template" 8 | 9 | _ "github.com/go-sql-driver/mysql" 10 | ) 11 | 12 | type Names struct { 13 | Id int 14 | Name string 15 | Email string 16 | } 17 | 18 | func dbConn() (db *sql.DB) { 19 | dbDriver := "mysql" 20 | dbUser := "MYSQL USERNAME" 21 | dbPass := "MYSQL PASSWORD" 22 | dbName := "DATABSE NAME" 23 | db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName) 24 | if err != nil { 25 | panic(err.Error()) 26 | } 27 | return db 28 | } 29 | 30 | var tmpl = template.Must(template.ParseGlob("tmpl/*")) 31 | 32 | func Index(w http.ResponseWriter, r *http.Request) { 33 | db := dbConn() 34 | selDB, err := db.Query("SELECT * FROM names ORDER BY id DESC") 35 | if err != nil { 36 | panic(err.Error()) 37 | } 38 | n := Names{} 39 | res := []Names{} 40 | for selDB.Next() { 41 | var id int 42 | var name, email string 43 | err = selDB.Scan(&id, &name, &email) 44 | if err != nil { 45 | panic(err.Error()) 46 | } 47 | n.Id = id 48 | n.Name = name 49 | n.Email = email 50 | res = append(res, n) 51 | } 52 | tmpl.ExecuteTemplate(w, "Index", res) 53 | defer db.Close() 54 | } 55 | 56 | func Show(w http.ResponseWriter, r *http.Request) { 57 | db := dbConn() 58 | nId := r.URL.Query().Get("id") 59 | selDB, err := db.Query("SELECT * FROM names WHERE id=?", nId) 60 | if err != nil { 61 | panic(err.Error()) 62 | } 63 | n := Names{} 64 | for selDB.Next() { 65 | var id int 66 | var name, email string 67 | err = selDB.Scan(&id, &name, &email) 68 | if err != nil { 69 | panic(err.Error()) 70 | } 71 | n.Id = id 72 | n.Name = name 73 | n.Email = email 74 | } 75 | tmpl.ExecuteTemplate(w, "Show", n) 76 | defer db.Close() 77 | } 78 | 79 | func New(w http.ResponseWriter, r *http.Request) { 80 | tmpl.ExecuteTemplate(w, "New", nil) 81 | } 82 | 83 | func Edit(w http.ResponseWriter, r *http.Request) { 84 | db := dbConn() 85 | nId := r.URL.Query().Get("id") 86 | selDB, err := db.Query("SELECT * FROM names WHERE id=?", nId) 87 | if err != nil { 88 | panic(err.Error()) 89 | } 90 | n := Names{} 91 | for selDB.Next() { 92 | var id int 93 | var name, email string 94 | err = selDB.Scan(&id, &name, &email) 95 | if err != nil { 96 | panic(err.Error()) 97 | } 98 | n.Id = id 99 | n.Name = name 100 | n.Email = email 101 | } 102 | tmpl.ExecuteTemplate(w, "Edit", n) 103 | defer db.Close() 104 | } 105 | 106 | func Insert(w http.ResponseWriter, r *http.Request) { 107 | db := dbConn() 108 | if r.Method == "POST" { 109 | name := r.FormValue("name") 110 | email := r.FormValue("email") 111 | insForm, err := db.Prepare("INSERT INTO names(name, email) VALUES(?,?)") 112 | if err != nil { 113 | panic(err.Error()) 114 | } 115 | insForm.Exec(name, email) 116 | log.Println("INSERT: Name: " + name + " | E-mail: " + email) 117 | } 118 | defer db.Close() 119 | http.Redirect(w, r, "/", 301) 120 | } 121 | 122 | func Update(w http.ResponseWriter, r *http.Request) { 123 | db := dbConn() 124 | if r.Method == "POST" { 125 | name := r.FormValue("name") 126 | email := r.FormValue("email") 127 | id := r.FormValue("uid") 128 | insForm, err := db.Prepare("UPDATE names SET name=?, email=? WHERE id=?") 129 | if err != nil { 130 | panic(err.Error()) 131 | } 132 | insForm.Exec(name, email, id) 133 | log.Println("UPDATE: Name: " + name + " | E-mail: " + email) 134 | } 135 | defer db.Close() 136 | http.Redirect(w, r, "/", 301) 137 | } 138 | 139 | func Delete(w http.ResponseWriter, r *http.Request) { 140 | db := dbConn() 141 | nId := r.URL.Query().Get("id") 142 | delForm, err := db.Prepare("DELETE FROM names WHERE id=?") 143 | if err != nil { 144 | panic(err.Error()) 145 | } 146 | delForm.Exec(nId) 147 | log.Println("DELETE") 148 | defer db.Close() 149 | http.Redirect(w, r, "/", 301) 150 | } 151 | 152 | func main() { 153 | log.Println("Server started on: http://localhost:9000") 154 | http.HandleFunc("/", Index) 155 | http.HandleFunc("/show", Show) 156 | http.HandleFunc("/new", New) 157 | http.HandleFunc("/edit", Edit) 158 | http.HandleFunc("/insert", Insert) 159 | http.HandleFunc("/update", Update) 160 | http.HandleFunc("/delete", Delete) 161 | http.ListenAndServe(":9000", nil) 162 | } 163 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "database/sql" // Database SQL package to perform queries 5 | "log" // Display messages to console 6 | "net/http" // Manage URL 7 | "text/template" // Manage HTML files 8 | 9 | _ "github.com/go-sql-driver/mysql" // MySQL Database driver 10 | ) 11 | 12 | // Struct used to send data to template 13 | // this struct is the same as the database 14 | type Names struct { 15 | Id int 16 | Name string 17 | Email string 18 | } 19 | 20 | // Function dbConn opens connection with MySQL driver 21 | // send the parameter `db *sql.DB` to be used by another functions 22 | func dbConn() (db *sql.DB) { 23 | 24 | dbDriver := "mysql" // Database driver 25 | dbUser := "" // Mysql username 26 | dbPass := "" // Mysql password 27 | dbName := "" // Mysql schema 28 | 29 | // Realize the connection with mysql driver 30 | db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName) 31 | 32 | // If error stop the application 33 | if err != nil { 34 | panic(err.Error()) 35 | } 36 | 37 | // Return db object to be used by other functions 38 | return db 39 | } 40 | 41 | // Read all templates on folder `tmpl/*` 42 | var tmpl = template.Must(template.ParseGlob("tmpl/*")) 43 | 44 | // Function Index shows all values on home 45 | func Index(w http.ResponseWriter, r *http.Request) { 46 | // Open database connection 47 | db := dbConn() 48 | 49 | // Prepare a SQL query to select all data from database and threat errors 50 | selDB, err := db.Query("SELECT * FROM names ORDER BY id DESC") 51 | if err != nil { 52 | panic(err.Error()) 53 | } 54 | 55 | // Call the struct to be rendered on template 56 | n := Names{} 57 | 58 | // Create a slice to store all data from struct 59 | res := []Names{} 60 | 61 | // Read all rows from database 62 | for selDB.Next() { 63 | // Must create this variables to store temporary query 64 | var id int 65 | var name, email string 66 | 67 | // Scan each row storing values from the variables above and check for errors 68 | err = selDB.Scan(&id, &name, &email) 69 | if err != nil { 70 | panic(err.Error()) 71 | } 72 | 73 | // Get the Scan into the Struct 74 | n.Id = id 75 | n.Name = name 76 | n.Email = email 77 | 78 | // Join each row on struct inside the Slice 79 | res = append(res, n) 80 | 81 | } 82 | 83 | // Execute template `Index` from `tmpl/*` folder and send the struct 84 | // (View the file: `tmpl/Index` 85 | tmpl.ExecuteTemplate(w, "Index", res) 86 | 87 | // Close database connection 88 | defer db.Close() 89 | } 90 | 91 | // Function Show displays a single value 92 | func Show(w http.ResponseWriter, r *http.Request) { 93 | // Open database connection 94 | db := dbConn() 95 | 96 | // Get the URL `?id=X` parameter 97 | nId := r.URL.Query().Get("id") 98 | 99 | // Perform a SELECT query getting the register Id(See above) and check for errors 100 | selDB, err := db.Query("SELECT * FROM names WHERE id=?", nId) 101 | if err != nil { 102 | panic(err.Error()) 103 | } 104 | 105 | // Call the struct to be rendered on template 106 | n := Names{} 107 | 108 | // Read all rows from database 109 | // This time we are going to get only one value, doesn't need the slice 110 | for selDB.Next() { 111 | // Store query values on this temporary variables 112 | var id int 113 | var name, email string 114 | 115 | // Scan each row to match the ID and check for errors 116 | err = selDB.Scan(&id, &name, &email) 117 | if err != nil { 118 | panic(err.Error()) 119 | } 120 | 121 | // Get the Scan into the Struct 122 | n.Id = id 123 | n.Name = name 124 | n.Email = email 125 | 126 | } 127 | 128 | // Execute template `Show` from `tmpl/*` folder and send the struct 129 | // (View the file: `tmpl/Show`) 130 | tmpl.ExecuteTemplate(w, "Show", n) 131 | 132 | // Close database connection 133 | defer db.Close() 134 | 135 | } 136 | 137 | // Function New just parse a form to send data to Insert function 138 | // (View the file: `tmpl/New`) 139 | func New(w http.ResponseWriter, r *http.Request) { 140 | tmpl.ExecuteTemplate(w, "New", nil) 141 | } 142 | 143 | // Function Edit works like Show 144 | // Only select the values to send to the Edit page Form 145 | // (View the file: `tmpl/Edit`) 146 | func Edit(w http.ResponseWriter, r *http.Request) { 147 | db := dbConn() 148 | 149 | // Get the URL `?id=X` parameter 150 | nId := r.URL.Query().Get("id") 151 | 152 | selDB, err := db.Query("SELECT * FROM names WHERE id=?", nId) 153 | if err != nil { 154 | panic(err.Error()) 155 | } 156 | 157 | n := Names{} 158 | 159 | for selDB.Next() { 160 | var id int 161 | var name, email string 162 | 163 | err = selDB.Scan(&id, &name, &email) 164 | if err != nil { 165 | panic(err.Error()) 166 | } 167 | 168 | n.Id = id 169 | n.Name = name 170 | n.Email = email 171 | 172 | } 173 | 174 | tmpl.ExecuteTemplate(w, "Edit", n) 175 | 176 | defer db.Close() 177 | } 178 | 179 | // Function Insert puts data into the database 180 | func Insert(w http.ResponseWriter, r *http.Request) { 181 | 182 | // Open database connection 183 | db := dbConn() 184 | 185 | // Check the request form METHOD 186 | if r.Method == "POST" { 187 | 188 | // Get the values from Form 189 | name := r.FormValue("name") 190 | email := r.FormValue("email") 191 | 192 | // Prepare a SQL INSERT and check for errors 193 | insForm, err := db.Prepare("INSERT INTO names(name, email) VALUES(?,?)") 194 | if err != nil { 195 | panic(err.Error()) 196 | } 197 | 198 | // Execute the prepared SQL, getting the form fields 199 | insForm.Exec(name, email) 200 | 201 | // Show on console the action 202 | log.Println("INSERT: Name: " + name + " | E-mail: " + email) 203 | } 204 | 205 | // Close database connection 206 | defer db.Close() 207 | 208 | // Redirect to HOME 209 | http.Redirect(w, r, "/", 301) 210 | } 211 | 212 | // Function Update, update values from database, 213 | // It's the same as Insert and New 214 | func Update(w http.ResponseWriter, r *http.Request) { 215 | 216 | db := dbConn() 217 | 218 | if r.Method == "POST" { 219 | 220 | // Get the values from form 221 | name := r.FormValue("name") 222 | email := r.FormValue("email") 223 | id := r.FormValue("uid") // This line is a hidden field on form (View the file: `tmpl/Edit`) 224 | 225 | // Prepare the SQL Update 226 | insForm, err := db.Prepare("UPDATE names SET name=?, email=? WHERE id=?") 227 | if err != nil { 228 | panic(err.Error()) 229 | } 230 | 231 | // Update row based on hidden form field ID 232 | insForm.Exec(name, email, id) 233 | 234 | // Show on console the action 235 | log.Println("UPDATE: Name: " + name + " | E-mail: " + email) 236 | } 237 | 238 | defer db.Close() 239 | 240 | // Redirect to Home 241 | http.Redirect(w, r, "/", 301) 242 | } 243 | 244 | // Function Delete destroys a row based on ID 245 | func Delete(w http.ResponseWriter, r *http.Request) { 246 | 247 | db := dbConn() 248 | 249 | // Get the URL `?id=X` parameter 250 | nId := r.URL.Query().Get("id") 251 | 252 | // Prepare the SQL Delete 253 | delForm, err := db.Prepare("DELETE FROM names WHERE id=?") 254 | if err != nil { 255 | panic(err.Error()) 256 | } 257 | 258 | // Execute the Delete SQL 259 | delForm.Exec(nId) 260 | 261 | // Show on console the action 262 | log.Println("DELETE") 263 | 264 | defer db.Close() 265 | 266 | // Redirect a HOME 267 | http.Redirect(w, r, "/", 301) 268 | } 269 | 270 | func main() { 271 | 272 | // Show on console the application stated 273 | log.Println("Server started on: http://localhost:9000") 274 | 275 | // URL management 276 | // Manage templates 277 | http.HandleFunc("/", Index) // INDEX :: Show all registers 278 | http.HandleFunc("/show", Show) // SHOW :: Show only one register 279 | http.HandleFunc("/new", New) // NEW :: Form to create new register 280 | http.HandleFunc("/edit", Edit) // EDIT :: Form to edit register 281 | 282 | // Manage actions 283 | http.HandleFunc("/insert", Insert) // INSERT :: New register 284 | http.HandleFunc("/update", Update) // UPDATE :: Update register 285 | http.HandleFunc("/delete", Delete) // DELETE :: Destroy register 286 | 287 | // Start the server on port 9000 288 | http.ListenAndServe(":9000", nil) 289 | 290 | } 291 | -------------------------------------------------------------------------------- /tmpl/Edit.tmpl: -------------------------------------------------------------------------------- 1 | {{ define "Edit" }} 2 | {{ template "Header" }} 3 | {{ template "Menu" }} 4 |