Simple Crud With golang and mysql
Prepare and Import MySQL driver into your project Using Terminal first install driver for Go's MySQL database package. Run below command and install MySQL driver's
go get -u github.com/go-sql-driver/mysql
Now create go_crud Database
- Open PHPMyAdmin/SQLyog or what ever MySQL database management tool that you are using.
- Create a new database "go_crud"
Creating the Users Table Execute the following SQL query to create a table named users inside your MySQL database. We will use this table for all of our future operations.
DROP TABLE IF EXISTS users;
CREATE TABLE users
(
id int(6) unsigned NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL,
age varchar(30) NOT NULL,
PRIMARY KEY (id)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8;
- You can use your own db driver and configure. just open
main.go
and editdbConn()
function:
func dbConn() (db *sql.DB) {
dbDriver := "mysql"
dbUser := "root"
dbPass := ""
dbName := "go_crud"
db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName)
if err != nil {
panic(err.Error())
}
return db
}
Run the following command in terminal
go run main.go