The compliment is much appreciated. TH-cam likes to penalize those with inconsistent upload schedules, either way I should be able to upload more consistently come January. I have a good like to dislike ratio but unfortunately TH-cam is getting rid of the dislike counter. In short, I will need to work harder, as long as people enjoy the videos, I will have the motivation needed. Thank you.
This is a fantastic tutorial but am I crazy or did you entirely skip the SQL setup? Your example works out of the box because you seem to have mysql already configured.
You are unfortunately correct, lol. Thank you for bringing it to my attention. I went ahead and posted my Golang and MySQL playlist in the description. Here is a quick look at the table looks like th-cam.com/video/a3YRzhyy3VY/w-d-xo.html and here is the playlist for Golang and MySQL th-cam.com/play/PLDZ_9qD1hkzOQdLHOPHtDcxoDSr0nno9G.html Again, thank you for bringing this to my attention and I will try to not miss this next time, thanks.
Pointers point to a memory address where a value can be accessed. If the pointer has a valid memory address saved in it, then great, the memory address can be found, and the value saved there can be accessed. You are getting an error because your code is looking for a memory address inside the pointer, but it is nil or not a valid memory address. There are several pointers in the code, *template.Template, *sql.DB, *http.Request, *sql.Stmt, to name a few. You should check to see that these are being populated correctly, for instance, make sure that db.Prepare() is returning a valid *sql.Stmt without error (error == nil). I have ran my code and are not getting the error so there must be a slight difference somewhere. I can't see your code so I can't see exactly what the problem is. You may need to post on Stack Overflow or provide more details here. I hope you find the solution, let me know how it goes. Best of luck and happy coding.
Good question, we do need email validation and it's warrants it's own video, hopefully I can find some time this week. Instead of using the unicode package we should utilize the flexibility of regular expressions with the regexp package. After checking the email address with a regular expression we should also check that the domain exists. This is incase the user types something that could be a valid email address but the domain doesn't actually exist. To do this we would use the net package, the function net.LookupMX() will allow us to look up the DNS MX record and it will return an error if not found.
Here is my video on how to use MySQL workbench th-cam.com/video/Mm71M3B-h5Y/w-d-xo.html and related videos in my Golang Web with MySQL playlist at th-cam.com/video/sWIyOCL2tI8/w-d-xo.html There is also more info on this particular database data in the previous videos in this Golang Web Dev playlist at th-cam.com/video/LsL6J5qk6Zc/w-d-xo.html I hope this was helpful, cheers!
In terminal: go get -u golang.org/x/crypto/bcrypt Make sure to have package in imports as well: import ( "database/sql" "fmt" "html/template" "net/http" "unicode" "golang.org/x/crypto/bcrypt" _ "github.com/go-sql-driver/mysql" )
@@MamaShareStories updated the code on Github so that it has error checking when creating pointer variables, so go ahead and pull the newest version. I hope this helps.
Would you recommend rendering templates in this fashion and just calling renderTemplate func in handler function? var templates = template.Must(template.ParseFiles("tmpl/login.html", "tmpl/welcome.html")) func renderTemplate(w http.ResponseWriter, tmpl string) { err := templates.ExecuteTemplate(w, tmpl+".html", nil) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }
- I like that you have the template.Must(), I like how it has to work error free or let you know immediately, much easier knowing there was an error than trying to debug later. - template.ParseFiles() works great, although your project grows and you will start adding many more templates, you will have to keep adding the file names to the function. I would look at template.ParseGlob(), which will just take a pattern and saves you from adding a file name each time. - I see your renderTemplate() checks for errors every time (something my video should have had in it, lol) which is good - The renderTemplate only takes one line of code to in initiate, which is nice - For when renderTemplate encounters an error I would recommend adding a log.Println() or some kind of logging for easier debugging in production - This suggestion I haven't seen in any videos yet but is important to some. Before reaching an error the template.ExectuteTemplate will start executing the ResponseWriter, writing a 200 status code. Unfortunately, if we encounter an error later, the OK status code has already been sent and the user may get part of a template. If you only want to write only after you know the entire template is good, then you could write to a temporary buffer and then copy to ResponseWriter after template.Execute ran with no error. This one is wordy and deserves its own video. I like it, the function wraps up a lot of reusable code for us, saving us from the extra typing. :)
This channel is so underrated. Really liked your content. Please continue to produce.
The compliment is much appreciated. TH-cam likes to penalize those with inconsistent upload schedules, either way I should be able to upload more consistently come January. I have a good like to dislike ratio but unfortunately TH-cam is getting rid of the dislike counter. In short, I will need to work harder, as long as people enjoy the videos, I will have the motivation needed. Thank you.
how to open this database in mysql workbench
Great thanks
You are very welcome!
how to open this database in mysql workbench
Thank you so much.
Always welcome
This is a fantastic tutorial but am I crazy or did you entirely skip the SQL setup? Your example works out of the box because you seem to have mysql already configured.
You are unfortunately correct, lol. Thank you for bringing it to my attention. I went ahead and posted my Golang and MySQL playlist in the description. Here is a quick look at the table looks like th-cam.com/video/a3YRzhyy3VY/w-d-xo.html and here is the playlist for Golang and MySQL th-cam.com/play/PLDZ_9qD1hkzOQdLHOPHtDcxoDSr0nno9G.html
Again, thank you for bringing this to my attention and I will try to not miss this next time, thanks.
It is showing error of invalid memory address or nil pointer dereference
How to resolve this error
Pointers point to a memory address where a value can be accessed. If the pointer has a valid memory address saved in it, then great, the memory address can be found, and the value saved there can be accessed. You are getting an error because your code is looking for a memory address inside the pointer, but it is nil or not a valid memory address. There are several pointers in the code, *template.Template, *sql.DB, *http.Request, *sql.Stmt, to name a few. You should check to see that these are being populated correctly, for instance, make sure that db.Prepare() is returning a valid *sql.Stmt without error (error == nil). I have ran my code and are not getting the error so there must be a slight difference somewhere. I can't see your code so I can't see exactly what the problem is. You may need to post on Stack Overflow or provide more details here. I hope you find the solution, let me know how it goes. Best of luck and happy coding.
I have copied same code from your github
I can send you code
If you can give your mail id
Resolve the error
Hi,
Which function of unicode package can be used to validate email Id's.
Good question, we do need email validation and it's warrants it's own video, hopefully I can find some time this week. Instead of using the unicode package we should utilize the flexibility of regular expressions with the regexp package. After checking the email address with a regular expression we should also check that the domain exists. This is incase the user types something that could be a valid email address but the domain doesn't actually exist. To do this we would use the net package, the function net.LookupMX() will allow us to look up the DNS MX record and it will return an error if not found.
how to open this database in mysql workbench
Here is my video on how to use MySQL workbench
th-cam.com/video/Mm71M3B-h5Y/w-d-xo.html and related videos in my Golang Web with MySQL playlist at th-cam.com/video/sWIyOCL2tI8/w-d-xo.html
There is also more info on this particular database data in the previous videos in this Golang Web Dev playlist at th-cam.com/video/LsL6J5qk6Zc/w-d-xo.html
I hope this was helpful, cheers!
How to import bcrypt package
In terminal:
go get -u golang.org/x/crypto/bcrypt
Make sure to have package in imports as well:
import (
"database/sql"
"fmt"
"html/template"
"net/http"
"unicode"
"golang.org/x/crypto/bcrypt"
_ "github.com/go-sql-driver/mysql"
)
Please resolve my error
I will be replying to this on the other comment so others can follow the troubleshooting as well.
@@GrowAdept same issue 0: runtime error: invalid memory address or nil pointer dereference
goroutine 34 [running]:
@@MamaShareStories updated the code on Github so that it has error checking when creating pointer variables, so go ahead and pull the newest version. I hope this helps.
Would you recommend rendering templates in this fashion and just calling renderTemplate func in handler function?
var templates = template.Must(template.ParseFiles("tmpl/login.html", "tmpl/welcome.html"))
func renderTemplate(w http.ResponseWriter, tmpl string) {
err := templates.ExecuteTemplate(w, tmpl+".html", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
- I like that you have the template.Must(), I like how it has to work error free or let you know immediately, much easier knowing there was an error than trying to debug later.
- template.ParseFiles() works great, although your project grows and you will start adding many more templates, you will have to keep adding the file names to the function. I would look at template.ParseGlob(), which will just take a pattern and saves you from adding a file name each time.
- I see your renderTemplate() checks for errors every time (something my video should have had in it, lol) which is good
- The renderTemplate only takes one line of code to in initiate, which is nice
- For when renderTemplate encounters an error I would recommend adding a log.Println() or some kind of logging for easier debugging in production
- This suggestion I haven't seen in any videos yet but is important to some. Before reaching an error the template.ExectuteTemplate will start executing the ResponseWriter, writing a 200 status code. Unfortunately, if we encounter an error later, the OK status code has already been sent and the user may get part of a template. If you only want to write only after you know the entire template is good, then you could write to a temporary buffer and then copy to ResponseWriter after template.Execute ran with no error. This one is wordy and deserves its own video.
I like it, the function wraps up a lot of reusable code for us, saving us from the extra typing. :)
@@GrowAdept Thank you for the thorough response, your videos are a hidden gem.
Much appreciated :)