Mình thấy go có kiểu con trỏ và không con trỏ, mình toàn dùng không con trỏ, vậy anh em hay dùng con trỏ cho trường hợp nào?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Nếu không sử dụng con trỏ thì mỗi lần truyền giá trị vào hàm nó lại sinh ra một bản copy, như vậy tương đối tốn bộ nhớ. Vậy nên đối với những struct nhiều dữ liệu thì nên sử dụng con trỏ.
Tuy nhiên hãy cẩn thận, vì sử dụng con trỏ cũng đồng nghĩa với việc dữ liệu trong đối tương có thể bị thay đổi trong hàm gọi, và có thể khiến cho logic của chúng ta bị sai.
Ví dụ, mình có 1 hàm readBook, và mình không kì vọng nó sẽ đổi dữ liệu, tuy nhiên có ai đó đã vô tình làm sai kiểu này:
Và dẫn đến dữ liệu book của mình bị sai.