ToB企服应用市场:ToB评测及商务社交产业平台

标题: 云端golang开发,无需本地配置,能上网就能开发和运行 [打印本页]

作者: 没腿的鸟    时间: 2023-11-21 04:17
标题: 云端golang开发,无需本地配置,能上网就能开发和运行
欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
需求

背景


前提


打造环境


设置GO111MODULE

  1. go env -w GO111MODULE=on
复制代码
验证之一:helloworld

  1. go mod init test003
复制代码
  1. go get -u github.com/gin-gonic/gin
复制代码
  1. package main
  2. import "github.com/gin-gonic/gin"
  3. func main() {
  4.         router := gin.Default()
  5.         router.GET("/", func(c *gin.Context) {
  6.                 c.JSON(200, gin.H{
  7.                         "message": "hello world",
  8.                 })
  9.         })
  10.         router.Run()
  11. }
复制代码
  1. @zq2599 ➜ /workspaces/blog_demos/tutorials/test003 (dev) $ go run main.go
  2. [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
  3. [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
  4. - using env:   export GIN_MODE=release
  5. - using code:  gin.SetMode(gin.ReleaseMode)
  6. [GIN-debug] GET    /                         --> main.main.func1 (3 handlers)
  7. [GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
  8. Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
  9. [GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
  10. [GIN-debug] Listening and serving HTTP on :8080
  11. [GIN] 2023/02/04 - 14:10:22 | 200 |        58.7µs | 220.246.254.226 | GET      "/"
  12. [GIN] 2023/02/04 - 14:10:22 | 404 |         900ns | 220.246.254.226 | GET      "/favicon.ico"
复制代码
验证之二:docker部署MySQL

  1. docker run \
  2. --name mysql \
  3. -p 3306:3306 \
  4. -e MYSQL_ROOT_PASSWORD=123456 \
  5. -d \
  6. mariadb:10.3
复制代码
  1. docker exec -it mysql /bin/bash
复制代码
  1. mysql -uroot -p123456
复制代码
  1. create database demo;
  2. use demo;
复制代码
  1. go get -u gorm.io/gorm
  2. go get -u gorm.io/driver/mysql
复制代码
  1. package main
  2. import (
  3.         "fmt"
  4.         "strconv"
  5.         "github.com/gin-gonic/gin"
  6.         "gorm.io/driver/mysql"
  7.         "gorm.io/gorm"
  8. )
  9. type Student struct {
  10.         gorm.Model
  11.         Name string
  12.         Age  uint64
  13. }
  14. // 全局数据库 db
  15. var db *gorm.DB
  16. // 包初始化函数,可以用来初始化 gorm
  17. func init() {
  18.         // 账号
  19.         username := "root"
  20.         // 密码
  21.         password := "123456"
  22.         // mysql 服务地址
  23.         host := "127.0.0.1"
  24.         // 端口
  25.         port := 3306
  26.         // 数据库名
  27.         Dbname := "demo"
  28.         // 拼接 mysql dsn,即拼接数据源,下方 {} 中的替换参数即可
  29.         // {username}:{password}@tcp({host}:{port})/{Dbname}?charset=utf8&parseTime=True&loc=Local&timeout=10s&readTimeout=30s&writeTimeout=60s
  30.         // timeout 是连接超时时间,readTimeout 是读超时时间,writeTimeout 是写超时时间,可以不填
  31.         dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", username, password, host, port, Dbname)
  32.         // err
  33.         var err error
  34.         // 连接 mysql 获取 db 实例
  35.         db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
  36.         if err != nil {
  37.                 panic("连接数据库失败, error=" + err.Error())
  38.         }
  39.         // 设置数据库连接池参数
  40.         sqlDB, _ := db.DB()
  41.         // 设置数据库连接池最大连接数
  42.         sqlDB.SetMaxOpenConns(10)
  43.         // 连接池最大允许的空闲连接数,如果没有sql任务需要执行的连接数大于2,超过的连接会被连接池关闭
  44.         sqlDB.SetMaxIdleConns(2)
  45.         // 建表
  46.         db.AutoMigrate(&Student{})
  47. }
  48. func main() {
  49.         router := gin.Default()
  50.         router.GET("/", func(c *gin.Context) {
  51.                 c.JSON(200, gin.H{
  52.                         "message": "hello world",
  53.                 })
  54.         })
  55.         router.GET("/create", func(c *gin.Context) {
  56.                 name := c.DefaultQuery("name", "小王子")
  57.                 ageStr := c.DefaultQuery("age", "1")
  58.                 var age uint64
  59.                 var err error
  60.                 if age, err = strconv.ParseUint(ageStr, 10, 32); err != nil {
  61.                         age = 1
  62.                 }
  63.                 fmt.Printf("name [%v], age [%v]\n", name, age)
  64.                 student := &Student{
  65.                         Name: name,
  66.                         Age:  age,
  67.                 }
  68.                 if err := db.Create(student).Error; err != nil {
  69.                         c.JSON(500, gin.H{
  70.                                 "code":    0,
  71.                                 "message": "insert db error",
  72.                         })
  73.                         return
  74.                 }
  75.                 c.JSON(200, gin.H{
  76.                         "code":    0,
  77.                         "message": fmt.Sprintf("insert db success [%+v]", student.Model.ID),
  78.                 })
  79.         })
  80.         router.Run()
  81. }
复制代码
  1. root@5e9f15ab9ac1:/# mysql -uroot -p123456
  2. Welcome to the MariaDB monitor.  Commands end with ; or \g.
  3. Your MariaDB connection id is 42
  4. Server version: 10.3.37-MariaDB-1:10.3.37+maria~ubu2004 mariadb.org binary distribution
  5. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  6. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  7. MariaDB [(none)]> use demo;
  8. Reading table information for completion of table and column names
  9. You can turn off this feature to get a quicker startup with -A
  10. Database changed
  11. MariaDB [demo]> select * from students;
  12. +----+-------------------------+-------------------------+------------+------+------+
  13. | id | created_at              | updated_at              | deleted_at | name | age  |
  14. +----+-------------------------+-------------------------+------------+------+------+
  15. |  1 | 2023-02-05 02:05:56.733 | 2023-02-05 02:05:56.733 | NULL       | Tom  |   10 |
  16. |  2 | 2023-02-05 02:09:35.537 | 2023-02-05 02:09:35.537 | NULL       | Tom  |   10 |
  17. |  3 | 2023-02-05 02:10:43.815 | 2023-02-05 02:10:43.815 | NULL       | Tom  |   10 |
  18. |  4 | 2023-02-05 02:10:45.069 | 2023-02-05 02:10:45.069 | NULL       | Tom  |   10 |
  19. |  5 | 2023-02-05 02:10:45.717 | 2023-02-05 02:10:45.717 | NULL       | Tom  |   10 |
  20. |  6 | 2023-02-05 02:10:46.000 | 2023-02-05 02:10:46.000 | NULL       | Tom  |   10 |
  21. |  7 | 2023-02-05 02:10:46.213 | 2023-02-05 02:10:46.213 | NULL       | Tom  |   10 |
  22. |  8 | 2023-02-05 02:10:46.578 | 2023-02-05 02:10:46.578 | NULL       | Tom  |   10 |
  23. |  9 | 2023-02-05 02:10:46.780 | 2023-02-05 02:10:46.780 | NULL       | Tom  |   10 |
  24. | 10 | 2023-02-05 02:10:46.976 | 2023-02-05 02:10:46.976 | NULL       | Tom  |   10 |
  25. | 11 | 2023-02-05 02:10:47.155 | 2023-02-05 02:10:47.155 | NULL       | Tom  |   10 |
  26. | 12 | 2023-02-05 02:10:47.359 | 2023-02-05 02:10:47.359 | NULL       | Tom  |   10 |
  27. +----+-------------------------+-------------------------+------------+------+------+
  28. 12 rows in set (0.000 sec)
复制代码
桌面版

欢迎关注博客园:程序员欣宸

学习路上,你不孤单,欣宸原创一路相伴...

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4