Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Code Snippet to perform CRUD operation in Go Lang
package main
import (
_ ""
func main() {
// Connect to the "company_db" database.
db, err := sql.Open("postgres", "postgresql://root@localhost:26257/company_db?sslmode=disable")
if err != nil {
log.Fatal("error connecting to the database: ", err)
// Insert a row into the "tbl_employee" table.
if _, err := db.Exec(
`INSERT INTO tbl_employee (full_name, department, designation, created_at, updated_at)
VALUES ('Irshad', 'IT', 'Product Manager', NOW(), NOW());`); err != nil {
// Select Statement.
rows, err := db.Query("select employee_id, full_name FROM tbl_employee;")
if err != nil {
defer rows.Close()
for rows.Next() {
var employeeID int64
var fullName string
if err := rows.Scan(&employeeId, &fullName); err != nil {
fmt.Printf("Employee Id : %d \t Employee Name : %s\n", employeeId, fullName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment