golang

关于GOROOT、GOPATH、GOBIN、project目录

て烟熏妆下的殇ゞ 提交于 2019-12-07 13:55:41
GOROOT、GOPATH、GOBIN、project目录 前言:我觉得java程序员学golang很容易上手。关于GOROOT、GOPATH、GOBIN这些环境变量的设置,我隐约感觉到了java的影子(尽管我是一个C++程序员),唯一和java不同的是不能设置“.”。 另外,golang的设计也很明显的透露着“约定优于配置”的原则。这在java很多框架里面很常见。golang的环境变量设计也是如此。从android和golang我觉得google喜欢java。 不了解java的C++程序员,可以尝试全新去理解golang。 GOROOT golang安装路径。 GOPATH 官方解释,请google。go工作环境中常常用到的一个很重要的环境变量(这种设计类似java)。具体用途:go命令常常需要用到的,如go run,go install, go get等。允许设置多个路径,和各个系统环境多路径设置一样,windows用“;”,linux(mac)用“:”分隔。 在linux(Mac)下,为了方便,一般配置在~/.bash_profile中。 book:~ wukebing$ vi ~/.bash_profile //编辑 book:~ wukebing$ source ~/.bash_profile //编辑完成后,使立即生效 例如:我的GOPATH设置(MAC下)

Docker-使用Dockerfile创建镜像

流过昼夜 提交于 2019-12-07 13:16:56
1、基本结构 Dockerfile由一行行命令语句组成,并支持以#开头的注释行。例如: # This dockerfile uses the ubuntu image # VERSION 2 - EDITION 1 # Author: docker_user # Command format: Instruction [arguments / command ] .. # Base image to use, this nust be set as the first line FROM ubuntu # Maintainer: docker_user <docker_user at email.com> (@docker_user) MAINTAINER docker_user docker_user@email.com # Commands to update the image RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list RUN apt-get update && apt-get install -y nginx RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf # Commands

Docker源码解读:1.flag解读

你。 提交于 2019-12-07 08:31:45
我是怎么想到要先看docker中的flag呢,就是因为docker采用了c/s结构,而且daemon和client都是用同一个程序的,因此,为了做出区分,肯定是要用参数来区分的。先来看位于./docker/docker/docker.go下面的main函数代码: func main() { //第一次肯定是返回false的,因为没有任何initializer if reexec.Init() { return } // Set terminal emulation based on platform as required. stdin, stdout, stderr := term.StdStreams() logrus.SetOutput(stderr) flag.Merge(flag.CommandLine, clientFlags.FlagSet, commonFlags.FlagSet) flag.Usage = func() { fmt.Fprint(os.Stdout, "Usage: docker [OPTIONS] COMMAND [arg...]\n"+daemonUsage+" docker [ --help | -v | --version ]\n\n") fmt.Fprint(os.Stdout, "A self-sufficient runtime

goroutine, channel 和 CSP

瘦欲@ 提交于 2019-12-07 07:43:15
引子 老听 clojure 社区的人提起 core.async ,说它如何好用,如何简化了并发编程的模型,不由得勾起了我的好奇心,想了解一番其思想的源头:CSP 模型及受其启发的 goroutine 和 channel 。 CSP 模型 CSP 描述这样一种并发模型:多个Process 使用一个 Channel 进行通信, 这个 Channel 连结的 Process 通常是匿名的,消息传递通常是同步的(有别于 Actor Model)。 CSP 最早是由 Tony Hoare 在 1977 年提出,据说老爷子至今仍在更新这个理论模型,有兴趣的朋友可以自行查阅电子版本: http://www.usingcsp.com/cspbook.pdf 。 严格来说,CSP 是一门形式语言(类似于 ℷ calculus),用于描述并发系统中的互动模式,也因此成为一众面向并发的编程语言的理论源头,并衍生出了 Occam/Limbo/Golang… 而具体到编程语言,如 Golang,其实只用到了 CSP 的很小一部分,即理论中的 Process/Channel(对应到语言中的 goroutine/channel):这两个并发原语之间没有从属关系, Process 可以订阅任意个 Channel,Channel 也并不关心是哪个 Process 在利用它进行通信;Process 围绕

golang思考之运行速度

你离开我真会死。 提交于 2019-12-07 07:43:02
有些资料显示golang的运行速度很慢,比Java慢,有时比Python慢。学习吧测试发现golang的运行速度和Java差不多。 首先,使用各种语言编写同一个CPU密集的程序sum。 C(或C++) #include <stdio.h> #include <stdint.h> #include <time.h> int main(void){ int iN; int64_t jN; scanf("%d %ld",&iN,&jN); time_t timeBegin=time(NULL); int i; for(i=0;i<iN;++i){ int64_t sum=0; int64_t j; for(j=0;j<jN;++j){ sum+=j; } printf("sum:%ld\n",sum); } time_t timeEnd=time(NULL); printf("%ds\n",(int)(timeEnd-timeBegin)); return 0; } Golang package main import "fmt" func main() { for i := 0; i < 10; i++ { var sum int64 = 0 var j int64 for j = 0; j < 1000000000; j++ { sum += j } fmt.Println("sum

Debian Gnu/Linux8.5安装GOLANG环境笔记

≡放荡痞女 提交于 2019-12-07 03:18:35
1、下载平台相关GOLANG文件,因为朝内墙了GOLANG官网,只能使用朝内GOLANG热心小伙伴提供的下载方式 2、tar -zxvf go1.6.2.linux-amd64.tar.gz -C /usr/local 3、vi /etc/profile 添加如下内容(红字部分为添加内容): # /etc/profile: system-wide .profile file for the Bourne shell (sh(1)) # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...). if [ "`id -u`" -eq 0 ]; then PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin :/usr/local/go/bin " else PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games :/usr/local/go/bin " fi export PATH export GOPATH=$HOME/gowork 4、source /etc/profile 5、使用用户登录执行如下命令: mkdir $GOAPTH 创建GOPATH目录

golang之旅--接口 (interface)

百般思念 提交于 2019-12-07 02:58:14
#golang接口简介 go语言不是传统的面向对象语言,因此没有类和继承的概念。 go语言中的接口跟java中的接口有点类似,接口可以有多个实现。 go语言的接口是go的一种类型,用来指定一组方法(方法集),go的接口归根结底就是一个方法集合。 go语言实现接口的方式不像java一样用implements关键字来实现,而是只要实现了接口中的所有方法就认为是实现了接口,这是一种规范或者叫约定(隐式实现)。 go语言中的接口往往都很短,不会存在太多方法,一般只有0到5个。 #golang接口声明(定义) type 接口名 interface { Method1(param_list) return_type Method2(param_list) return_type ... } #golang接口使用 在Java中我们通过implements可以实现接口,那么在golang中怎么实现接口呢。 在go语言中并没有关键字来标识xx类型实现了xx接口,只是存在一个约定,当xx类型实现了接口中的所有方法,那么就说明该类型实现了xx接口。 注:即使接口在类型之后才定义,二者处于不同的包中,被单独编译:只要类型实现了接口中的方法,它就实现了此接口。 #golang接口实战 定义一个接口 type IHttpProxy interface { GET() POST() } 实现一个接口 type

如何快速搜索golang的包引用情况?

丶灬走出姿态 提交于 2019-12-06 22:15:34
命令如下: alias gos='gos(){echo $@:;grep --exclude-dir=vendor --include="*.go" -nPr "(\s|&)($@)\.[A-Z].*" || echo "nothing" ;echo "";};gos $@' 使用方法: 假设查看istio中对net包的使用情况 cd istio-1.3.5 gos net 输出如下: net: tools/istio-iptables/pkg/dependencies/implementation.go:34: addrs, err := net.InterfaceAddrs() tools/istio-iptables/pkg/dependencies/stub.go:33: return net.ParseIP(ip), nil tools/istio-iptables/main.go:48: ip, ipNet, err := net.ParseCIDR(ipRange) tools/istio-iptables/main.go:78: var enableInboundIPv6s net.IP 来源: https://www.cnblogs.com/futuretea/p/11999838.html

如何快速查看一个golang项目?

南笙酒味 提交于 2019-12-06 22:09:42
脚本如下 #!/usr/bin/env bash [[ -n $DEBUG ]] && set -x set -ou pipefail useage(){ cat <<"EOF" USAGE: gop.sh SRCPATH EOF } exit_err() { echo >&2 "${1}" exit 1 } if [ $# -ne 1 ];then useage exit fi SRCPATH=$1 if [ ! -d "${SRCPATH}" ];then echo "${SRCPATH} is not a dir" exit fi cd "${SRCPATH}" || return red='\033[0;31m' plain='\033[0m' info(){ echo -ne "\n${red}$1:${plain}\n" } filenovendor(){ find . -type f -name "$1" -not -path "./vendor/*" } grepnovendor(){ grep --exclude-dir=vendor --include="$1" -Por "$2" } grepnovendorz(){ grep --exclude-dir=vendor --include="$1" -Porz "$2" } info "仓库统计" git

[翻译] effective go 之 Data

喜你入骨 提交于 2019-12-06 21:52:29
Data Allocation with new Go has two allocation primitives, the built-in functions new and make. They do different things and apply to different types, which can be confusing, but the rules are simple. Let's talk about new first. It's a built-in function that allocates memory, but unlike its namesakes in some other languages it does not initialize the memory, it only zeros it. That is, new(T) allocates zeroed storage for a new item of type T and returns its address, a value of type *T. In Go terminology, it returns a pointer to a newly allocated zero value of type T. Go有两种分配内存的方式 new 和 make