@radovskyb Yes !! It worked!!! THANK you so much. I am completely new to programming and this was really dissuading me from actually learning how to program. You help is really appreciated. Incidentally HOW do you get the Yellow box around your code like that ? lol
@radovskyb It worked but it only said page not found 404. It is supposed to display some page data for the test.txt file I created. HOW would I do that?
After you paste or type your code in the editing box where I’m typing now, highlight the code you want in the box and then click the little </> icon. You can also simply indent all of your code by 4 spaces and it will automatically be boxed as well.
@radovskyb Ok. I will do that. Also It worked but it only said page not found 404. It is supposed to display some page data for the test.txt file I created. HOW would I do that?
Ok, so basically, you are now receiving that error because you are trying to load a page that doesn’t exist yet and the code I added for you lets you know that the page is missing with an http 404 error.
If I recall correctly, you are currently learning from this page: https://golang.org/doc/articles/wiki/, which in that case I simply suggest following and reading it all the way to the end properly because further down in the article you’ll even see that they add similar error handling to what I wrote for you earlier.
If you are positive that you have created the test.txt file in the correct place, make sure you are going to the correct link. The way the code is currently written, to view test.txt, you should be going to http://localhost:8080/view/test, but maybe you are accidentally going to http://localhost:8080/view/test.txt instead?
@iivri, it sounds like to me, that you have either created test.txt in the wrong directory or you are running your program from the wrong directory.
Try to make sure you are following that blog post properly because it should tell you what folder structure you should have set up and how to correctly run your program.
For example, if you are trying to open a file in the same directory where your code is located, but you run the program from outside of that folder, it will not work properly.
One other thing since you just mentioned before that you are new to programming.
Yeah, there’s no problem with that, but what I’m suggesting is that you make sure that you are actually running go run main.go from inside of the folder when you run your program, or if you are using go build or go install, when you actually run the executable for the program, that you are inside of the same folder
If you are unsure if your program is being called from the correct folder, add something like this to your main function and check if the printed location is the same as the location where your test.txt file is being stored.
Oh yep, no problem, the issue now is that you are accidentally using fmt.Println, instead of fmt.Fprintf
Replace:
fmt.Println(w, "%s%s", p.Title, p.Body)
With:
fmt.Fprintf(w, "%s%s", p.Title, p.Body)
With fmt.Println you are accidentally printing out the http.ResponseWriter object w which is why all of that stuff showed up, instead of with using fmt.Fprintf it will print the formatted result TO w which is a writer.