github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/database/sql/example_service_test.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package sql_test
     6  
     7  import (
     8  	"github.com/shogo82148/std/database/sql"
     9  	"github.com/shogo82148/std/log"
    10  	"github.com/shogo82148/std/net/http"
    11  )
    12  
    13  func Example_openDBService() {
    14  	// ドライバを開くと、通常はデータベースに接続しようとは試みません。
    15  	db, err := sql.Open("driver-name", "database=test1")
    16  	if err != nil {
    17  
    18  		// これは接続エラーではなく、DSNの解析エラーや他の初期化エラーです。
    19  		log.Fatal(err)
    20  	}
    21  	db.SetConnMaxLifetime(0)
    22  	db.SetMaxIdleConns(50)
    23  	db.SetMaxOpenConns(50)
    24  
    25  	s := &Service{db: db}
    26  
    27  	http.ListenAndServe(":8080", s)
    28  }