赞
踩
这里分类和汇总了欣宸的全部原创(含配套源码):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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。