0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Go: gorm で PostgreSQL のデータを削除 (Delete)

Last updated at Posted at 2023-03-03

プログラム

フォルダー構造

$ tree -a
.
├── .env
├── config_postgres.go
└── gorm_pg_delete.go
gorm_pg_delete.go
// ----------------------------------------------------------------
//
//	gorm_pg_delete.go
//
//					Mar/03/2023
//
// ----------------------------------------------------------------
package main

import (
	"fmt"
	"os"
	"gorm.io/gorm"
	"gorm.io/driver/postgres"
)


/*
type City struct {
	Id           string
	Name         string
	Population   int
	Date_mod     string
}

	cities := []City{}
*/


// ----------------------------------------------------------------
func main() {
	fmt.Fprintln (os.Stderr,"*** 開始 ***")
	key_in := os.Args[1]

	fmt.Printf ("key_in = %s\n" , key_in)

	host,user,password,data_base := config_postgres_proc()

dsn := "host=" + host + " user=" + user + " password=" + password + " dbname=" + data_base + " port=5432 sslmode=disable TimeZone=Asia/Tokyo"

db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
		}


	db.Exec ("delete from cities WHERE id = ?", key_in)

	fmt.Fprintln (os.Stderr,"*** 終了 ***")
}

// ----------------------------------------------------------------

.env
config_postgres.go
はこちら
Go: gorm で PostgreSQL のバージョンを表示

実行の準備

go mod init delete
go mod tidy

go.mod go.sum が作成される。

実行結果

$ go run gorm_pg_delete.go config_postgres.go t3463
*** 開始 ***
key_in = t3463
*** 終了 ***
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?