go

How to set the environment variable in windows?

走远了吗. 提交于 2021-02-17 07:07:05
问题 Below Go code: var ( Address = os.Getenv("ADDR") Token = os.Getenv("TOKEN") ) reads the environment variables in windows In windows laptop, I have privilege to set user variables for my user login only. I created two variables(for above), but os.Getenv() cannot read the values. I do not have privilege to set system variables How to set environment variables in windows, with my user login? 回答1: In Windows, environment variables can be applied in two ways. Set modifies the current shell's (the

golang运算与循环等

半城伤御伤魂 提交于 2021-02-17 06:58:54
一、golang运算符 1、算术运算符 + 相加 - 相减 * 相乘 / 相除 % 求余 ++ 自增 -- 自减 2、关系运算符 == 等于 != 不等于 > 大于 < 小于 >= 大于等于 <= 小于等于 3、位运算符 & 按位与运算符"&"是双目运算符。 其功能是参与运算的两数各对应的二进位相与。 | 按位或运算符"|"是双目运算符。 其功能是参与运算的两数各对应的二进位相或。 ^ 按位异或运算符"^"是双目运算符。 其功能是参与运算的两数各对应的二进位相异或,当两对应的二进位相异时,结果为1。 << 左移运算符"<<"是双目运算符。左移n位就是乘以2的n次方。 其功能把"<<"左边的运算数的各二进位全部左移若干位,由"<<"右边的数指定移动的位数,高位丢弃,低位补0。 >> 右移运算符">>"是双目运算符。右移n位就是除以2的n次方。 其功能是把">>"左边的运算数的各二进位全部右移若干位,">>"右边的数指定移动的位数。 4、逻辑运算符 && 与 || 或 !(感叹号) 非 5、赋值运算符 += (C += A 等于 C = C + A) -= (C -= A 等于 C = C - A) *= (C *= A 等于 C = C * A) /= (C /= A 等于 C = C / A) %= (C %= A 等于 C = C % A) 6、运算符优先级 二、golang

Go 1.15 正式发布

…衆ロ難τιáo~ 提交于 2021-02-17 06:53:49
就在昨天,也就是2020年8月11日,go开发团队发布了go最新版本1.15。该版本在 1.14 的基础上继续改进工具链、运行时和库。也保留了GO1兼容性的承诺。这几乎保证所有的go程序都能像以前那样的正常编译与运行。并且在Go 1.15中对链接器也有重大改进,改进了对具有大量内核的小对象的分配,并弃用了 X.509 CommonName。GOPROXY 现在支持跳过返回错误的代理,并添加了新的嵌入式 tzdata 包。 编译器 包 unsafe 的安全规则允许在调用某些函数时将 unsafe.Pointer 转换为 uintptr 。以前,在某些情况下,编译器允许进行多次链接转换(例如 syscall.Syscall(…uintptr(uintptr(ptr)), …) )。现在,编译器只需要一次转换。使用多次转换的代码应进行更新以满足安全规则。与 Go 1.14 相比,Go 1.15 通过消除某些类型的 GC 元数据并更积极地消除了未使用的类型元数据,与 Go 1.14 相比将典型的二进制大小减少了大约5%。该工具链现在通过将函数与 32 字节边界对齐并填充跳转指令来缓解 GOARCH=amd64 上的 Intel CPU 勘误 SKX102 。尽管此填充增加了二进制大小,但这远远超出了上述二进制大小改进所弥补的范围。Go 1.15在编译器和汇编器中都添加了一个标志

5分钟学Go 基础01:初识 Go 的第一印象是薪水可观

心已入冬 提交于 2021-02-17 06:46:33
最近几年,随着云计算、区块链等技术的发展,有一门语言逐渐成为大家热衷讨论的对象,成为了继 Python 之后的另一块香饽饽,那就是 Go 语言。 其实如果要追溯历史的话,Go 语言诞生于 Google,从 2007 年便开始设计,并于 2009 年 11 月 10 日正式发布第一版,此后便开始在 Google 内部使用。 正如 Python 借助 AI 的东风大火一样,Go 语言大范围的使用和普及也是借助了云计算、区块链等技术的东风,特别是云计算,以 Google 开源的 Kubernetes 为代表,Go 逐渐成为了云计算领域的第一首选语言,目前还没有竞争对手。 国内最早全面使用 Go 语言的公司要属七牛云了,其创始人许式伟说过一句话:“Go 语言是互联网时代的 C 语言”。可想它如今的地位是不言而喻的。 下图是 2020 StackOverflow 的调查,左边是最流行的语言排行榜,右边是最热门的语言。流行看的是历史地位和应用程度,而热门更多看的是近来的关注程度。 最流行的语言 最热门的语言 可以看到,Go 语言应用范围具有一定的局限性,所以流行程度并不高,但关注程度是相对较高的,特别是对比前几年的数据你会发现,它是一直增长的。 接下来再来看下大家比较关心的一个问题:哪种语言的薪资较高?毕竟程序员这个行业,回归到现实,都是“面对薪资编程”。如下图所示,左边是 2019 年的数据

Sending email from Office365 with STARTTLS fails

孤人 提交于 2021-02-17 06:20:08
问题 I am trying to send an email from an Office365 server but I become the following error: panic: tls: first record does not look like a TLS handshake The account configuration is the following smtp.office365.com:587 (STARTTLS). For the authentication an username+password is needed. The code I am using is pretty similar to all the examples I saw in the web but I can't get it to work. It fails at tls.Dial. func Mail() { mail := Mail{} mail.senderId = "theemail@example.com" mail.toIds = []string{

Why can't I initialize a map with new() in Go?

主宰稳场 提交于 2021-02-17 06:01:33
问题 package main import "fmt" func main() { p := new(map[string]int) m := make(map[string]int) m["in m"] = 2 (*p)["in p"] = 1 fmt.Println(m) fmt.Println(*p) } The above code gives an error panic: assignment to entry in nil map . If I print *p before inserting pairs into it, the output is correct. It seems I just can't modify *p ? 回答1: Both new and make are used to allocate memory in a program, but they work differently. new(T, args) zeros memory and returns the memory address (a value of type *T

What is an idiomatic way of representing enums in Go?

旧城冷巷雨未停 提交于 2021-02-17 05:49:31
问题 I'm trying to represent a simplified chromosome, which consists of N bases, each of which can only be one of {A, C, T, G} . I'd like to formalize the constraints with an enum, but I'm wondering what the most idiomatic way of emulating an enum is in Go. 回答1: Quoting from the language specs:Iota Within a constant declaration, the predeclared identifier iota represents successive untyped integer constants. It is reset to 0 whenever the reserved word const appears in the source and increments

How does go know where to get packages from

萝らか妹 提交于 2021-02-17 05:37:49
问题 When I import a package in a go application (using modules and vscode with golang plugin): import ( cors "goa.design/plugins/cors/dsl" ) go automatically retrieves the package. How does go know where to get it from? I figured it simply pulls the sources from github.com, but the repository for this project is at https://github.com/goadesign/goa no . in the name. And if I change the import to: import ( cors "goa.design/plugins/v3/cors/dsl" ) It retrieves the v3 package. I would gladly RTFM, but

Is there any profits of using Methods rather than Functions?

僤鯓⒐⒋嵵緔 提交于 2021-02-17 05:00:15
问题 I'm developing "Matrix" struct and related methods for the purpose of practicing Go. I made a lot of methods but I realized that all these methods can be changed into functions I'm used to C++ and in C++, if I make a function whose parameter is a type of class, the function cannot use the class' private variable(information hiding) However, when I built a similar code using "Go", a function can access a struct's variable. So I don't get what is different between methods and functions in Go.

Where does Go define that p.Field (as opposed to (*p).Field) is a valid syntax even when p is a pointer to a struct?

寵の児 提交于 2021-02-17 02:17:23
问题 Here is my Go program. package main import ( "fmt" ) type Person struct { Name string } func main() { p := &Person{"Jack"} // The following statement makes sense. We dereference the // pointer to reach the Person object and then retrieve its Name // field. fmt.Println((*p).Name) // But the following statement does not make sense. How does // this code work by retrieving the Name field directly from the // pointer without dereferencing it? fmt.Println(p.Name) } Here is the output. $ go run foo