在本教程中,您将在共享的多模块工作区中创建两个模块,对这些模块进行更改,并在构建中查看这些更改的结果。
本教程需要 go1.18 或更高版本。使用go.dev/dl中的链接确保您已在 Go 1.18 或更高版本中安装了 Go 。
首先,为您要编写的代码创建一个模块。
1、打开命令提示符并切换到您的主目录。
在 Linux 或 Mac 上:
在 Windows 上:
2、在命令提示符下,为您的代码创建一个名为工作区的目录。
3、初始化模块
我们的示例将创建一个hello依赖于 golang.org/x/example 模块的新模块。
创建你好模块:
使用 . 添加对 golang.org/x/example 模块的依赖项go get。
在 hello 目录下创建 hello.go,内容如下:
现在,运行 hello 程序:
在这一步中,我们将创建一个go.work文件来指定模块的工作区。
在workspace目录中,运行:
该go work init命令告诉为包含目录中模块的工作空间go创建一个文件 。go.work./hello
该go命令生成一个go.work如下所示的文件:
该go.work文件的语法与go.mod相同。
该go指令告诉 Go 应该使用哪个版本的 Go 来解释文件。它类似于文件中的go指令go.mod 。
该use指令告诉 Go在进行构建时hello目录中的模块应该是主模块。
所以在模块的任何子目录中workspace都会被激活。
2、运行工作区目录下的程序
在workspace目录中,运行:
Go 命令包括工作区中的所有模块作为主模块。这允许我们在模块中引用一个包,即使在模块之外。在模块或工作区之外运行go run命令会导致错误,因为该go命令不知道要使用哪些模块。
接下来,我们将golang.org/x/example模块的本地副本添加到工作区。然后,我们将向stringutil包中添加一个新函数,我们可以使用它来代替Reverse.
在这一步中,我们将下载包含该模块的 Git 存储库的副本golang.org/x/example,将其添加到工作区,然后向其中添加一个我们将从 hello 程序中使用的新函数。
1、克隆存储库
在工作区目录中,运行git命令来克隆存储库:
2、将模块添加到工作区
该go work use命令将一个新模块添加到 go.work 文件中。它现在看起来像这样:
该模块现在包括example.com/hello模块和 `golang.org/x/example 模块。
这将允许我们使用我们将在模块副本中编写的新代码,而不是使用命令stringutil下载的模块缓存中的模块版本。
3、添加新功能。
我们将向golang.org/x/example/stringutil包中添加一个新函数以将字符串大写。
将新文件夹添加到workspace/example/stringutil包含以下内容的目录:
4、修改hello程序以使用该功能。
修改workspace/hello/hello.go的内容以包含以下内容:
从工作区目录,运行
Go 命令在go.work文件指定的hello目录中查找命令行中指定的example.com/hello模块 ,同样使用go.work文件解析导入golang.org/x/example。
go.work可以用来代替添加replace 指令以跨多个模块工作。
由于这两个模块在同一个工作区中,因此很容易在一个模块中进行更改并在另一个模块中使用它。
现在,要正确发布这些模块,我们需要发布golang.org/x/example 模块,例如在v0.1.0. 这通常通过在模块的版本控制存储库上标记提交来完成。发布完成后,我们可以增加对 golang.org/x/example模块的要求hello/go.mod:
这样,该go命令可以正确解析工作区之外的模块。
很多朋友可能知道Go语言的优势在哪,却不知道Go语言适合用于哪些地方。
1、 Go语言作为服务器编程语言,很适合处理日志、数据打包、虚拟机处理、文件系统、分布式系统、数据库代理等;网络编程方面。Go语言广泛应用于Web应用、API应用、下载应用等;除此之外,Go语言还可用于内存数据库和云平台领域,目前国外很多云平台都是采用Go开发。
2、 其实Go语言主要用作服务器端开发。其定位是用来开发"大型软件"的,适合于很多程序员一起开发大型软件,并且开发周期长,支持云计算的网络服务。Go语言能够让程序员快速开发,并且在软件不断的增长过程中,它能让程序员更容易地进行维护和修改。它融合了传统编译型语言的高效性和脚本语言的易用性和富于表达性。
3、 Go语言成功案例。Nsq:Nsq是由Go语言开发的高性能、高可用消息队列系统,性能非常高,每天能处理数十亿条的消息;
4、 Docker:基于lxc的一个虚拟打包工具,能够实现PAAS平台的组建。
5、 Packer:用来生成不同平台的镜像文件,例如VM、vbox、AWS等,作者是vagrant的作者
6、 Skynet:分布式调度框架。
7、 Doozer:分布式同步工具,类似ZooKeeper。
8、 Heka:mazila开源的日志处理系统。
9、 Cbfs:couchbase开源的分布式文件系统。
10、 Tsuru:开源的PAAS平台,和SAE实现的功能一模一样。
11、 Groupcache:memcahe作者写的用于Google下载系统的缓存系统。
12、 God:类似redis的缓存系统,但是支持分布式和扩展性。
13、 Gor:网络流量抓包和重放工具。
以上的就是关于go语言能做什么的内容介绍了。
可以配合UUID或者GUID来实现GUID是一个128位长的数字,一般用16进制表示。算法的核心思想是结合机器的网卡、当地时间、一个随机数来生成GUID。从理论上讲,如果一台机器每秒产生10000000个GUID,则可以保证(概率意义上)3240年不重复。
UUID是1.5中新增的一个类,在java.util下,用它可以产生一个号称全球唯一的ID
import java.util.UUID
public class Test {
public static void main(String[] args) {
UUID uuid = UUID.randomUUID()
System.out.println (uuid)
}
}
编译运行输出:
07ca3dec-b674-41d0-af9e-9c37583b08bb
两种方式生成guid 与uuid
需要comm log 库
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.net.InetAddress
import java.net.UnknownHostException
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import java.security.SecureRandom
import java.util.Random
public class RandomGUID extends Object {
protected final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
.getLog(getClass())
public String valueBeforeMD5 = ""
public String valueAfterMD5 = ""
private static Random myRand
private static SecureRandom mySecureRand
private static String s_id
private static final int PAD_BELOW = 0x10
private static final int TWO_BYTES = 0xFF
/*
* Static block to take care of one time secureRandom seed.
* It takes a few seconds to initialize SecureRandom. You might
* want to consider removing this static block or replacing
* it with a "time since first loaded" seed to reduce this time.
* This block will run only once per JVM instance.
*/
static {
mySecureRand = new SecureRandom()
long secureInitializer = mySecureRand.nextLong()
myRand = new Random(secureInitializer)
try {
s_id = InetAddress.getLocalHost().toString()
} catch (UnknownHostException e) {
e.printStackTrace()
}
}
/*
* Default constructor. With no specification of security option,
* this constructor defaults to lower security, high performance.
*/
public RandomGUID() {
getRandomGUID(false)
}
/*
* Constructor with security option. Setting secure true
* enables each random number generated to be cryptographically
* strong. Secure false defaults to the standard Random function seeded
* with a single cryptographically strong random number.
*/
public RandomGUID(boolean secure) {
getRandomGUID(secure)
}
/*
* Method to generate the random GUID
*/
private void getRandomGUID(boolean secure) {
MessageDigest md5 = null
StringBuffer sbValueBeforeMD5 = new StringBuffer(128)
try {
md5 = MessageDigest.getInstance("MD5")
} catch (NoSuchAlgorithmException e) {
logger.error("Error: " + e)
}
try {
long time = System.currentTimeMillis()
long rand = 0
if (secure) {
rand = mySecureRand.nextLong()
} else {
rand = myRand.nextLong()
}
sbValueBeforeMD5.append(s_id)
sbValueBeforeMD5.append(":")
sbValueBeforeMD5.append(Long.toString(time))
sbValueBeforeMD5.append(":")
sbValueBeforeMD5.append(Long.toString(rand))
valueBeforeMD5 = sbValueBeforeMD5.toString()
md5.update(valueBeforeMD5.getBytes())
byte[] array = md5.digest()
StringBuffer sb = new StringBuffer(32)
for (int j = 0j <array.length++j) {
int b = array[j] &TWO_BYTES
if (b <PAD_BELOW)
sb.append('0')
sb.append(Integer.toHexString(b))
}
valueAfterMD5 = sb.toString()
} catch (Exception e) {
logger.error("Error:" + e)
}
}
/*
* Convert to the standard format for GUID
* (Useful for SQL Server UniqueIdentifiers, etc.)
* Example: C2FEEEAC-CFCD-11D1-8B05-00600806D9B6
*/
public String toString() {
String raw = valueAfterMD5.toUpperCase()
StringBuffer sb = new StringBuffer(64)
sb.append(raw.substring(0, 8))
sb.append("-")
sb.append(raw.substring(8, 12))
sb.append("-")
sb.append(raw.substring(12, 16))
sb.append("-")
sb.append(raw.substring(16, 20))
sb.append("-")
sb.append(raw.substring(20))
return sb.toString()
}
// Demonstraton and self test of class
public static void main(String args[]) {
for (int i=0i<100i++) {
RandomGUID myGUID = new RandomGUID()
System.out.println("Seeding String=" + myGUID.valueBeforeMD5)
System.out.println("rawGUID=" + myGUID.valueAfterMD5)
System.out.println("RandomGUID=" + myGUID.toString())
}
}
}