goadmin 学习笔记

1.安装命令行

Following three steps to run it.

Note: now you can quickly start by doing like this.

$ go install github.com/GoAdminGroup/adm@latest
$ mkdir new_project && cd new_project
$ adm init

Or (use adm whose version higher or equal than v1.2.16)

$ mkdir new_project && cd new_project
$ go install github.com/GoAdminGroup/adm@latest
$ adm init web

查看版本

new@bogon src % adm -V    
GoAdmin CLI v1.2.24

goadmin 学习笔记_第1张图片

goadmin 学习笔记_第2张图片问题1: 拒绝访问: http://127.0.0.1:9033/admin/login

goadmin 学习笔记_第3张图片

原因1:找不到包,执行go run main.go 后,按照提示 go  get  包

package main

import (
	"context"
	"errors"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"

	_ "github.com/GoAdminGroup/go-admin/adapter/gin"              // web framework adapter
	_ "github.com/GoAdminGroup/go-admin/modules/db/drivers/mysql" // sql driver
	_ "github.com/GoAdminGroup/themes/adminlte"                // ui theme

	"github.com/GoAdminGroup/go-admin/engine"
	"github.com/GoAdminGroup/go-admin/template"
	"github.com/GoAdminGroup/go-admin/template/chartjs"
	"github.com/gin-gonic/gin"

	"goadmin/models"  //此处goadmin换成你项目的名字
	"goadmin/pages"   //此处goadmin换成你项目的名字
	"goadmin/tables"  //此处goadmin换成你项目的名字,此处被坑过
)

func main() {
	startServer()
}

func startServer() {
	gin.SetMode(gin.ReleaseMode)
	gin.DefaultWriter = ioutil.Discard

	r := gin.Default()

	template.AddComp(chartjs.NewChart())

	eng := engine.Default()

	if err := eng.AddConfigFromJSON("./config.json").
		AddGenerators(tables.Generators).
		Use(r); err != nil {
		panic(err)
	}

	r.Static("/uploads", "./uploads")

	eng.HTML("GET", "/admin", pages.GetDashBoard)
	eng.HTMLFile("GET", "/admin/hello", "./html/hello.tmpl", map[string]interface{}{
		"msg": "Hello world",
	})

	models.Init(eng.MysqlConnection())

	srv := &http.Server{
		Addr:    ":9033",
		Handler: r,
	}

	go func() {
		if err := srv.ListenAndServe(); err != nil && errors.Is(err, http.ErrServerClosed) {
			log.Printf("listen: %s\n", err)
		}
	}()

	quit := make(chan os.Signal, 1)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit
	log.Println("Shutting down server...")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	if err := srv.Shutdown(ctx); err != nil {
		log.Fatal("Server forced to shutdown:", err)
	}
	log.Print("closing database connection")
	eng.MysqlConnection().Close()

	log.Println("Server exiting")
}
"goadmin/models" //此处goadmin换成你项目的名字,因为名字不匹配,导致引入包时,找不到包。

问题2:go 版本与goadmin 版本不兼容,需要升级go 版本

因为我的go 版本是1.17 下载的goadmin 是 v1.2.24

goadmin 学习笔记_第4张图片

goadmin 学习笔记_第5张图片

goadmin 学习笔记_第6张图片

区分几个路径
  • GOROOT:Golang的安装路径,安装之后就默认自带了
  • GOPATH:Golang的工作目录

问题3: 安装go 1.20版本
$ go install golang.org/dl/go1.20@latest
$ go1.20 download

参考:https://blog.csdn.net/perfumekristy/article/details/128880591

go1.17 与go1.20 两版本共存。

goadmin 学习笔记_第7张图片

goadmin 学习笔记_第8张图片

参考文档:http://doc.go-admin.cn/zh/install/

https://blog.csdn.net/qq_42647903/article/details/123253707

https://github.com/GoAdminGroup/go-admin  ​​​​​​

你可能感兴趣的:(GO,学习,笔记)