当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

谈谈Go的“无依赖”

发表于: 2012-01-05   作者:bookjovi   来源:转载   浏览:
Go
摘要: Go语言与现在的主流语言相比绝对算是个异类,不说concurrent了,就说说它的"无依赖"   Go的"无依赖"特性主要体现在以下几点:   1)pkg以静态库.a的形式存在,这与C/C++ package完全不同   [root@localhost linux_386]# pwd /root/Go/go/pkg

Go语言与现在的主流语言相比绝对算是个异类,不说concurrent了,就说说它的"无依赖"

 

Go的"无依赖"特性主要体现在以下几点:

 

1)pkg以静态库.a的形式存在,这与C/C++ package完全不同

 

[root@localhost linux_386]# pwd
/root/Go/go/pkg/linux_386
[root@localhost linux_386]# ls
archive      compress   encoding  fmt.a   html.a   io.a    math.a  old      path.a     runtime.a  strings.a  testing.a  websocket.a
bufio.a      container  errors.a  go      image    life.a  mime    os       reflect.a  runtime.h  sync       text
bytes.a      crypto     exp       hash    image.a  log     mime.a  os.a     regexp     sort.a     sync.a     time.a
cgocall.h    crypto.a   expvar.a  hash.a  index    log.a   net     patch.a  regexp.a   stdio.a    syscall.a  unicode
cgosotest.a  debug      flag.a    html    io       math    net.a   path     runtime    strconv.a  testing    unicode.a

2)编译链接后的go程序(一般像8.out或6.out)无依赖,就连lib.so也不用link,这导致go程序的执行完全独立于外部环境

   [更新:有些go程序需要连接libc.so,应为package里import "C"了]

 

[root@localhost test]# file 8.out 
8.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped
[root@localhost test]# ldd 8.out 
        not a dynamic executable

3)正是有了2)的要求,导致Go runtime的实现完全不依赖与libc,所以所有的syscall要go自己实现,内存分配也要自己实现(go heap使用的page size大都是64k,而不是默认的4k,有点像hugetbl,同时go也实现了per-thread的内存分配),thread tls也要go自己实现。

 

一点感想:go的大部分核心功能是在runtime静态库中实现的(里面实现了内存分配器,channel,scheduler,goroutine,def/panic/recover),那么这个runtime.a有没有可能被其他语言或软件项目重用呢?

 

少了libc.so的参与,go运行程序的strace非常清爽,没有多余的一大堆syscall

 

execve("./8.out", ["./8.out"], [/* 33 vars */]) = 0
modify_ldt(1, {entry_number:7, base_addr:0x810ec20, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}, 16) = 0
rt_sigaction(SIGQUIT, {0x805be22, ~[RT_2 RT_8 RT_12 RT_13 RT_16 RT_18 RT_20 RT_21 RT_22 RT_23 RT_24 RT_25 RT_26 RT_27 RT_29 RT_30 RT_31], SA_RESTORER|SA_STACK|SA_SIGINFO, 0x805be6f}, NULL, 8) = 0
rt_sigaction(SIGILL, {0x805be22, ~[RT_2 RT_8 RT_12 RT_13 RT_16 RT_18 RT_20 RT_21 RT_22 RT_23 RT_24 RT_25 RT_26 RT_27 RT_29 RT_30 RT_31], SA_RESTORER|SA_STACK|SA_SIGINFO, 0x805be6f}, NULL, 8) = 0
rt_sigaction(SIGTRAP, {0x805be22, ~[RT_2 RT_8 RT_12 RT_13 RT_16 RT_18 RT_20 RT_21 RT_22 RT_23 RT_24 RT_25 RT_26 RT_27 RT_29 RT_30 RT_31], SA_RESTORER|SA_STACK|SA_SIGINFO, 0x805be6f}, NULL, 8) = 0
rt_sigaction(SIGABRT, {0x805be22, ~[RT_2 RT_8 RT_12 RT_13 RT_16 RT_18 RT_20 RT_21 RT_22 RT_23 RT_24 RT_25 RT_26 RT_27 RT_29 RT_30 RT_31], SA_RESTORER|SA_STACK|SA_SIGINFO, 0x805be6f}, NULL, 8) = 0
rt_sigaction(SIGBUS, {0x805be22, ~[RT_2 RT_8 RT_12 RT_13 RT_16 RT_18 RT_20 RT_21 RT_22 RT_23 RT_24 RT_25 RT_26 RT_27 RT_29 RT_30 RT_31], SA_RESTORER|SA_STACK|SA_SIGINFO, 0x805be6f}, NULL, 8) = 0
rt_sigaction(SIGFPE, {0x805be22, ~[RT_2 RT_8 RT_12 RT_13 RT_16 RT_18 RT_20 RT_21 RT_22 RT_23 RT_24 RT_25 RT_26 RT_27 RT_29 RT_30 RT_31], SA_RESTORER|SA_STACK|SA_SIGINFO, 0x805be6f}, NULL, 8) = 0
rt_sigaction(SIGSEGV, {0x805be22, ~[RT_2 RT_8 RT_12 RT_13 RT_16 RT_18 RT_20 RT_21 RT_22 RT_23 RT_24 RT_25 RT_26 RT_27 RT_29 RT_30 RT_31], SA_RESTORER|SA_STACK|SA_SIGINFO, 0x805be6f}, NULL, 8) = 0
rt_sigaction(SIGPIPE, {0x805be6e, ~[RT_2 RT_8 RT_12 RT_13 RT_16 RT_18 RT_20 RT_21 RT_22 RT_23 RT_24 RT_25 RT_26 RT_27 RT_29 RT_30 RT_31], SA_RESTORER|SA_STACK|SA_SIGINFO, 0x805be6f}, NULL, 8) = 0
rt_sigaction(SIGSTKFLT, {0x805be22, ~[RT_2 RT_8 RT_12 RT_13 RT_16 RT_18 RT_20 RT_21 RT_22 RT_23 RT_24 RT_25 RT_26 RT_27 RT_29 RT_30 RT_31], SA_RESTORER|SA_STACK|SA_SIGINFO, 0x805be6f}, NULL, 8) = 0
rt_sigaction(SIGSYS, {0x805be22, ~[RT_2 RT_8 RT_12 RT_13 RT_16 RT_18 RT_20 RT_21 RT_22 RT_23 RT_24 RT_25 RT_26 RT_27 RT_29 RT_30 RT_31], SA_RESTORER|SA_STACK|SA_SIGINFO, 0x805be6f}, NULL, 8) = 0
open("/proc/stat", O_RDONLY|O_CLOEXEC)  = 3
read(3, "cpu  31796 246 50794 19875689 10"..., 252) = 252
read(3, "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "..., 252) = 252
read(3, "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "..., 252) = 252
read(3, "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "..., 252) = 252
read(3, "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "..., 252) = 252
read(3, "nning 1\nprocs_blocked 0\nsoftirq "..., 252) = 98
read(3, "", 252)                        = 0
close(3)                                = 0
mmap2(0x8600000, 805306368, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x8600000
mmap2(NULL, 131072, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb778e000
mmap2(0x18600000, 1048576, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x18600000
mmap2(0x185e0000, 131072, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x185e0000
mmap2(NULL, 131072, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb776e000
mmap2(NULL, 131072, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb774e000
sigaltstack({ss_sp=0x18604000, ss_flags=0, ss_size=32768}, NULL) = 0
gettimeofday({1325098088, 59908}, NULL) = 0
mmap2(NULL, 1048576, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb764e000
gettimeofday({1325098088, 61747}, NULL) = 0
gettimeofday({1325098088, 62945}, NULL) = 0
gettimeofday({1325098088, 63401}, NULL) = 0

谈谈Go的“无依赖”

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号