我的SQL代码在MVC中应该放在哪里

I'm just starting to learn the MVC design pattern, and I was wondering where should my SQL code go.

For example, lets say I have a register form structure that looks like this

type Form struct {
 Username string
 Password string
}

I assume that the form structure is part of the model, so I have some function that goes with the form that after a user submits the form, the data gets put into the database, so my function would look something like this

func (f *Form) registerUser() {
   // SQL code goes here
}

Is this the best way of doing it? I have been searching around for open source Golang web apps that utilizes the MVC pattern, but I have not be able to find one which I completely understand.

In model-view-controller pattern...

Model is for entities all your classes represent real world objects.

View is the forms and all the graphic things user can see and interact with.

Controller is for controller classes, is all the logic of the program, for the sql code as you said you can implement a dao pattern and have all the sql code in the controller package and the database class in the entities package(i leave it in the Controller class).

I assume that the form structure is part of the model, so I have some function that goes with the form that after a user submits the form, the data gets put into the database, so my function would look something like this

Another use of Model in MVC application architecture is to store reusable code. So, yes, you can store the form in Model (for example, if you reuse it multiple in views) but this makes less sense than storing form in a View and reuse it later on.

The execution backtrace would be something like

  1. Controller processes the request - personally, I do the business logic here and also (if necessary) invoke ...
  2. Model that handles all the data retrieval from DBMS, validation, etc. and returns processed data to Controller and ...
  3. The View is then displayed with the respective parameters (user data, template, validator result, etc.).
  4. User fills in the form and submits the input to Controller and ...
  5. GOTO 1. point