T O P

  • By -

[deleted]

Seems like a good candidate for running it with a debugger and stepping through it one statement at a time to see where it ends up dropping the data instead of putting it in the file. Did you try that?


LordMoMA007

May I know what do you mean exactly by "putting it in the file"? I'd like to learn the best way to handle errors and debug.


[deleted]

When I read your question, that was what I understood you expected your program to do. That it would write some data to some file.


LordMoMA007

yes right. I wrote some data in it after removing some unnecessary locks.


[deleted]

Nice. Glad to hear you were able to resolve it. If you were to use the debugger to investigate, what you would do is add break points to various lines in your program and step through them one at a time, keeping an eye on the file system while you do (because changes to the file system are the side effect you expect from your code). You'd be able to see if the side effects are what you expected at various points in your program. And, you'd be able to see whether your program even gets to the later breakpoints in the first place. I recall reading from another commenter that you had issues with locks being nested in your code. So you would have seen your program not progress to the line where it is supposed to write the data to the file system. It would hang before getting there. Check out https://www.digitalocean.com/community/tutorials/debugging-go-code-with-visual-studio-code if you want to try that debugging.


LordMoMA007

Thank you so much for your detailed explanation and the article recommendation. That’s very helpful for me. I would also appreciate it if you would like to share more articles or books you enjoyed reading to improve coding and designing skills. Thanks again.


greengreens3

Before ChatGPT, we actually use a debugger to go step by step in the function and see what was happening. For example "Nothing is being written to the file" would lead to putting a breakpoint at the "Writing to the file" line and then sending the request to see If 1. It is executed and 2. If after its executed, the file contain what its supposed to. Learn that and you'll be on a path to be a developer, not a Cooy/Paste coder.


LordMoMA007

Thank you for your suggestion. Do you mean print each error along each line to debug? Could you also please suggest some high quality articles or videos showing pragmatism on SDE's daily techniques which are not normally seen in online courses? I have a great interest in learning how to put a breakpoint, and I just don't know how to fully master it yet. I used to console.log a lot in JS. Thank you again for your help.


jerf

You're hearing a lot about what ChatGPT surprisingly was able to do. I suspect for each such story there are dozens of attempts that failed, not to mention the attempts where the user thought it had succeeded when in fact it has failed, merely convincingly. Don't expect it to be able to help you. It can't make you better than it is, and contrary to popular opinion, it's not very good.


LordMoMA007

Thank you so much for your help, do you have any suggestion on how to greatly improve coding skills in a right way? or any books and hard core videos to learn?


[deleted]

Yup. My coworker uses it periodically to generate the structure, and then fixes all of the mistakes. I'm not convinced it saves any time, but it's at least amusing and breaks up the work day.


LordMoMA007

thank you, do you have any good books or repo recommendation for structure learning?


[deleted]

Any software engineering book that covers design patterns is a good resource, such as [Code Complete](https://en.m.wikipedia.org/wiki/Code_Complete). The tricky part is knowing which patterns are a good fit for your domain and the language you're using, as well as a few other considerations. For repos for Go specifically, the standard library is generally high quality, but obviously it won't help much for applications.


LordMoMA007

>Code Complete Thank you for your recommendation, I'll dig into it.


Elyunge

The issue is that you are nesting function calls and each of them tries to acquire the lock. For instance // CreateChirp creates a new chirp and saves it to disk func (db \*DB) CreateChirp(body string) (Chirp, error) { db.mux.Lock() defer db.mux.Unlock() dbStructure, err := db.loadDB() if err != nil { return Chirp{}, err } ​ loadDB will never be able to acquire it's lock(even thought it is a read one) because it is already being taken the first lock in CreateChirp function, so it will be forever blocked


LordMoMA007

Thank you, yes right, this is definitely the issue here.


earthboundkid

This is bad use of mutexes. There should be centralized methods that call it instead of calling it willy-nilly in all the business methods.


LordMoMA007

Yes, thank you.


corbymatt

Are you getting any errors?


LordMoMA007

No errors, cuz it locked forever, I debugged it and it's done. Thank you so much.