6

go学习笔记( 使用go module )导入本地包

 3 years ago
source link: https://surest.cn/archives/158/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

文档: https://studygolang.com/articles/24517?fr=sidebar

goLand 配置

图片描述...

配置全球代理

url: https://goproxy.io/zh/


vim ~/.bash_profile
> 添加环境变量
    
# 启用 Go Modules 功能
export GO111MODULE=on
# 配置 GOPROXY 环境变量
export GOPROXY=https://goproxy.io
    
source ~/.bash_profile
> OK
    

module 常用命令

  • go help mod
  • go mod init

导入本地包

如果项目有.git目录使用如下命令

go mod init 
go mod init packgeName

创建一个本地包

例如: ch > format.go

package ch

import (
    "fmt"
    "reflect"
    "time"
)

func Format(time time.Time) string  {
    fmt.Println(reflect.TypeOf(time))
    t := time.Format("2006-01-02 15:04:05")
    return t
}

func ParseTimestamp(t int64) time.Time {
    nt := time.Unix(t, 0)
    return nt
}
> main.go

package main

import (
    "fmt"
    "os"
    "time"
    "imp/ch"
)

func main()  {
    args := os.Args[1:]

    if len(args) == 0 {
        nowTime := time.Now()
        formatTime := ch.Format(nowTime)
        fmt.Println(formatTime)
        fmt.Println(nowTime)
        return
    }

    for _, arg := range args {
        fmt.Println(arg)
    }
}

> go.mod
> ----

module imp

go 1.13

replace imp/ch => ./ch

  • go mod init
  • go help mod
  • go mod tidy

本文由 邓尘锋 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Jun 20, 2020 at 10:46 pm


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK