源码开发
安装包管理第三方模块
go env -w GO111MODULE=on go mod init goweb go mod tidy go run main.go初始化go项目步骤
go env -w GO111MODULE=on
go mod init goweb 自动在当前目录创建go.mod文件 goweb是设置本项目的包名,可以自定义任何名称.外部项目导入本项目的时候需要用的就是这个包名
go mod tidy 自动下载本项目中所有用到的被依赖的第三方包。同时修改go.mod的内容并创建go.sum文件
grom的使用
package main import ( "gorm.io/driver/mysql" "gorm.io/gorm" "fmt" ) type Userinfo struct { Id uint Name string Gender string Hobby string } func main() { // 连接数据库 dsn := "root:aaaa@tcp(192.168.2.191:6306)/db1?charset=utf8mb4&parseTime=True&loc=Local" db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) if err != nil { panic(err) } //add(db) ListUser(db) } func add(db *gorm.DB) { //自动迁移 db.AutoMigrate(&Userinfo{}) u1 := Userinfo{Id: 3, Name: "张三222", Gender: "男", Hobby: "学习"} db.Create(&u1) //创建 } func ListUser(db *gorm.DB) { var users []Userinfo db.Find(&users) for _,user := range users { fmt.Println(user) fmt.Println(user.Name) } }数据库操作实例
Go读取Redis
[Redis] IpHost = "192.168.30.252:6379" Password = "Admin" KeyName = "ProbeService"config.toml
package utils import ( "github.com/BurntSushi/toml" ) var ( ConfPath string // Conf global Conf = &Config{} ) // Config . type Config struct { Redis *Redis } type Redis struct { IpHost string Password string KeyName string } func CfgInit() (err error) { _, err = toml.DecodeFile(ConfPath, &Conf) return }conf.go
package mynmap import ( "fmt" "yxh/scan/utils" "github.com/go-redis/redis" ) func (N *NmapProbe)RedisInit(){ apath, _:=os.Getwd() var cfgfile string if strings.Index(apath,"\\") > 0{ cfgfile = apath+"\\file\\config.toml" } else { cfgfile = apath+"/file/config.toml" } utils.ConfPath = cfgfile if err := utils.CfgInit(); err != nil { fmt.Println("utils.CfgInit() err:%+v", err) } redisConf := utils.Conf.Redis //fmt.Println(redisConf.KeyName) client := redis.NewClient(&redis.Options{ Addr: redisConf.IpHost, Password: redisConf.Password, DB: 0, }) ProbesInfo, _ := client.Get(redisConf.KeyName).Result() //fmt.Println(ProbesInfo) N.parseProbesFromContent(&ProbesInfo) N.parseProbesToMapKName() defer client.Close() } func (N *NmapProbe)FileInit(){ apath, _:=os.Getwd() //var nmapfile string var portfile string if strings.Index(apath,"\\") > 0{ //windows //nmapfile = apath+"\\file\\nmap.txt" portfile = apath+"\\file\\port.txt" } else { //linux os //nmapfile = apath+"/file/nmap.txt" portfile = apath+"/file/port.txt" } //statikFS, err := fs.New() //statikFS压缩后每次都必须要重新build main.go //改成由os直接读取磁盘文件,直接修改文件即可立即生效 /* filecon1, err := os.Open(nmapfile) if err != nil { fmt.Println("[-] 静态资源FS初始化失败:%s", err.Error()) } defer filecon1.Close() nmapContents, err := ioutil.ReadAll(filecon1) if err != nil{ fmt.Println("[-] 加载Nmap协议失败:%s", err.Error()) } */ filecon2, err := os.Open(portfile) if err != nil{ fmt.Println("[-] 加载端口数据失败:%s\n", err.Error()) } defer filecon2.Close() nmapContentsPort, err := ioutil.ReadAll(filecon2) if err != nil{ fmt.Println("[-] 加载端口数据失败:%s\n", err.Error()) } //strdata := string(nmapContents) //N.parseProbesFromContent(&strdata) // 解析nmap指纹库,读取nmap.txt //N.parseProbesToMapKName() N.ServiceParse(nmapContentsPort) // 解析端口对应协议数据,读取ports.txt } // nmap指纹库初始化 func (N *NmapProbe)Init(){ N.RedisInit() N.FileInit() }nmap.go
标签:Println,string,err,fmt,go,ORM,Go,apath,安装 From: https://www.cnblogs.com/yxh168/p/16771442.html