mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-08-08 16:21:43 +00:00
ch7: fix code format
This commit is contained in:
@@ -12,10 +12,10 @@
|
||||
import "database/sql"
|
||||
|
||||
func listTracks(db sql.DB, artist string, minYear, maxYear int) {
|
||||
result, err := db.Exec(
|
||||
"SELECT * FROM tracks WHERE artist = ? AND ? <= year AND year <= ?",
|
||||
artist, minYear, maxYear)
|
||||
// ...
|
||||
result, err := db.Exec(
|
||||
"SELECT * FROM tracks WHERE artist = ? AND ? <= year AND year <= ?",
|
||||
artist, minYear, maxYear)
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
@@ -23,22 +23,22 @@ Exec方法使用SQL字面量替換在査詢字符串中的每個'?';SQL字面
|
||||
|
||||
```go
|
||||
func sqlQuote(x interface{}) string {
|
||||
if x == nil {
|
||||
return "NULL"
|
||||
} else if _, ok := x.(int); ok {
|
||||
return fmt.Sprintf("%d", x)
|
||||
} else if _, ok := x.(uint); ok {
|
||||
return fmt.Sprintf("%d", x)
|
||||
} else if b, ok := x.(bool); ok {
|
||||
if b {
|
||||
return "TRUE"
|
||||
}
|
||||
return "FALSE"
|
||||
} else if s, ok := x.(string); ok {
|
||||
return sqlQuoteString(s) // (not shown)
|
||||
} else {
|
||||
panic(fmt.Sprintf("unexpected type %T: %v", x, x))
|
||||
}
|
||||
if x == nil {
|
||||
return "NULL"
|
||||
} else if _, ok := x.(int); ok {
|
||||
return fmt.Sprintf("%d", x)
|
||||
} else if _, ok := x.(uint); ok {
|
||||
return fmt.Sprintf("%d", x)
|
||||
} else if b, ok := x.(bool); ok {
|
||||
if b {
|
||||
return "TRUE"
|
||||
}
|
||||
return "FALSE"
|
||||
} else if s, ok := x.(string); ok {
|
||||
return sqlQuoteString(s) // (not shown)
|
||||
} else {
|
||||
panic(fmt.Sprintf("unexpected type %T: %v", x, x))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -48,11 +48,11 @@ switch語句可以簡化if-else鏈,如果這個if-else鏈對一連串值做相
|
||||
|
||||
```go
|
||||
switch x.(type) {
|
||||
case nil: // ...
|
||||
case int, uint: // ...
|
||||
case bool: // ...
|
||||
case string: // ...
|
||||
default: // ...
|
||||
case nil: // ...
|
||||
case int, uint: // ...
|
||||
case bool: // ...
|
||||
case string: // ...
|
||||
default: // ...
|
||||
}
|
||||
```
|
||||
|
||||
@@ -70,21 +70,21 @@ switch x := x.(type) { /* ... */ }
|
||||
|
||||
```go
|
||||
func sqlQuote(x interface{}) string {
|
||||
switch x := x.(type) {
|
||||
case nil:
|
||||
return "NULL"
|
||||
case int, uint:
|
||||
return fmt.Sprintf("%d", x) // x has type interface{} here.
|
||||
case bool:
|
||||
if x {
|
||||
return "TRUE"
|
||||
}
|
||||
return "FALSE"
|
||||
case string:
|
||||
return sqlQuoteString(x) // (not shown)
|
||||
default:
|
||||
panic(fmt.Sprintf("unexpected type %T: %v", x, x))
|
||||
}
|
||||
switch x := x.(type) {
|
||||
case nil:
|
||||
return "NULL"
|
||||
case int, uint:
|
||||
return fmt.Sprintf("%d", x) // x has type interface{} here.
|
||||
case bool:
|
||||
if x {
|
||||
return "TRUE"
|
||||
}
|
||||
return "FALSE"
|
||||
case string:
|
||||
return sqlQuoteString(x) // (not shown)
|
||||
default:
|
||||
panic(fmt.Sprintf("unexpected type %T: %v", x, x))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user