這裡分類和彙總了欣宸的全部原創(含配套原始碼):https://github.com/zq2599/blog_demos
go env -w GO111MODULE=on
go mod init test003
go get -u github.com/gin-gonic/gin
package main
import "github.com/gin-gonic/gin"
func main() {
router := gin.Default()
router.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "hello world",
})
})
router.Run()
}
@zq2599 ➜ /workspaces/blog_demos/tutorials/test003 (dev) $ go run main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET / --> main.main.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
[GIN] 2023/02/04 - 14:10:22 | 200 | 58.7µs | 220.246.254.226 | GET "/"
[GIN] 2023/02/04 - 14:10:22 | 404 | 900ns | 220.246.254.226 | GET "/favicon.ico"
docker run \
--name mysql \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=123456 \
-d \
mariadb:10.3
docker exec -it mysql /bin/bash
mysql -uroot -p123456
create database demo;
use demo;
go get -u gorm.io/gorm
go get -u gorm.io/driver/mysql
package main
import (
"fmt"
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type Student struct {
gorm.Model
Name string
Age uint64
}
// 全域性資料庫 db
var db *gorm.DB
// 包初始化函數,可以用來初始化 gorm
func init() {
// 賬號
username := "root"
// 密碼
password := "123456"
// mysql 服務地址
host := "127.0.0.1"
// 埠
port := 3306
// 資料庫名
Dbname := "demo"
// 拼接 mysql dsn,即拼接資料來源,下方 {} 中的替換引數即可
// {username}:{password}@tcp({host}:{port})/{Dbname}?charset=utf8&parseTime=True&loc=Local&timeout=10s&readTimeout=30s&writeTimeout=60s
// timeout 是連線超時時間,readTimeout 是讀超時時間,writeTimeout 是寫超時時間,可以不填
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", username, password, host, port, Dbname)
// err
var err error
// 連線 mysql 獲取 db 範例
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("連線資料庫失敗, error=" + err.Error())
}
// 設定資料庫連線池引數
sqlDB, _ := db.DB()
// 設定資料庫連線池最大連線數
sqlDB.SetMaxOpenConns(10)
// 連線池最大允許的空閒連線數,如果沒有sql任務需要執行的連線數大於2,超過的連線會被連線池關閉
sqlDB.SetMaxIdleConns(2)
// 建表
db.AutoMigrate(&Student{})
}
func main() {
router := gin.Default()
router.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "hello world",
})
})
router.GET("/create", func(c *gin.Context) {
name := c.DefaultQuery("name", "小王子")
ageStr := c.DefaultQuery("age", "1")
var age uint64
var err error
if age, err = strconv.ParseUint(ageStr, 10, 32); err != nil {
age = 1
}
fmt.Printf("name [%v], age [%v]\n", name, age)
student := &Student{
Name: name,
Age: age,
}
if err := db.Create(student).Error; err != nil {
c.JSON(500, gin.H{
"code": 0,
"message": "insert db error",
})
return
}
c.JSON(200, gin.H{
"code": 0,
"message": fmt.Sprintf("insert db success [%+v]", student.Model.ID),
})
})
router.Run()
}
root@5e9f15ab9ac1:/# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 42
Server version: 10.3.37-MariaDB-1:10.3.37+maria~ubu2004 mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use demo;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [demo]> select * from students;
+----+-------------------------+-------------------------+------------+------+------+
| id | created_at | updated_at | deleted_at | name | age |
+----+-------------------------+-------------------------+------------+------+------+
| 1 | 2023-02-05 02:05:56.733 | 2023-02-05 02:05:56.733 | NULL | Tom | 10 |
| 2 | 2023-02-05 02:09:35.537 | 2023-02-05 02:09:35.537 | NULL | Tom | 10 |
| 3 | 2023-02-05 02:10:43.815 | 2023-02-05 02:10:43.815 | NULL | Tom | 10 |
| 4 | 2023-02-05 02:10:45.069 | 2023-02-05 02:10:45.069 | NULL | Tom | 10 |
| 5 | 2023-02-05 02:10:45.717 | 2023-02-05 02:10:45.717 | NULL | Tom | 10 |
| 6 | 2023-02-05 02:10:46.000 | 2023-02-05 02:10:46.000 | NULL | Tom | 10 |
| 7 | 2023-02-05 02:10:46.213 | 2023-02-05 02:10:46.213 | NULL | Tom | 10 |
| 8 | 2023-02-05 02:10:46.578 | 2023-02-05 02:10:46.578 | NULL | Tom | 10 |
| 9 | 2023-02-05 02:10:46.780 | 2023-02-05 02:10:46.780 | NULL | Tom | 10 |
| 10 | 2023-02-05 02:10:46.976 | 2023-02-05 02:10:46.976 | NULL | Tom | 10 |
| 11 | 2023-02-05 02:10:47.155 | 2023-02-05 02:10:47.155 | NULL | Tom | 10 |
| 12 | 2023-02-05 02:10:47.359 | 2023-02-05 02:10:47.359 | NULL | Tom | 10 |
+----+-------------------------+-------------------------+------------+------+------+
12 rows in set (0.000 sec)
可能有些讀者對網頁面的IDE心存顧慮:操作流暢度和體驗方面與傳統桌面版有差距,或者說習慣了桌面版(主要是不像欣宸這麼窮,破電腦只夠執行瀏覽器),這時候還可以用本地桌面版遠端連線雲開發環境,這時候編碼在本地vscode,而編譯執行還在之前的雲環境進行,既解決了習慣問題,又不影響白嫖微軟伺服器,依舊是快樂滿滿,具體操作方法如下,點選紅色箭頭所指的選單
此時瀏覽器就會拉起本地vscode
拉起的過程可能沒那麼順利,會要求您的vscode登入GitHub賬號,然後再重新拉起,多折騰幾次就可以了,拉起後的效果如下,和在本地執行專案看不出區別
一切都符合預期,可見微軟誠不欺我,4核8G伺服器資源免費用,誠意滿滿
這下似乎找不到偷懶的理由了,電腦破沒關係,不想安裝設定也沒關係,沒有伺服器也沒關係,GitHub都為你準備好了,還有什麼理由不靜下心來認真學習呢?
瞭解更多codespaces詳情,請存取官方資料:https://docs.github.com/zh/codespaces/overview