Go1 两行代码编译生成超大EXE

99 views
Skip to first unread message

Liigo Zhuang

unread,
Mar 30, 2012, 2:58:16 AM3/30/12
to golang...@googlegroups.com
以下代码用到Go1标准库fmt和expvar,仅两行代码,结果编译生成的EXE超大,达 3.2 MB。我的系统是 Windows 7, x86。
按说,如果仅仅输出 fmt.Println("Hello world"),EXE应该在 1.1 MB 是比较正常的表现。

编译方法:
go build expvar.go

--------------------------------------------------------
package main

import (
"fmt"
"expvar"
)

func main() {
fmt.Println("Hello, Go1. This is liigo.")
expvar.Do(func(kv expvar.KeyValue){ fmt.Printf("\n%s=%s\n",kv.Key,kv.Value) })
}
--------------------------------------------------------

--
by Liigo, http://blog.csdn.net/liigo/

minux

unread,
Mar 30, 2012, 3:02:21 AM3/30/12
to golang...@googlegroups.com


2012/3/30 Liigo Zhuang <com....@gmail.com>

以下代码用到Go1标准库fmt和expvar,仅两行代码,结果编译生成的EXE超大,达 3.2 MB。我的系统是 Windows 7, x86。
按说,如果仅仅输出 fmt.Println("Hello world"),EXE应该在 1.1 MB 是比较正常的表现。

编译方法:
go build expvar.go

--------------------------------------------------------
package main

import (
"fmt"
"expvar"
)

func main() {
fmt.Println("Hello, Go1. This is liigo.")
expvar.Do(func(kv expvar.KeyValue){ fmt.Printf("\n%s=%s\n",kv.Key,kv.Value) })
}
问题是你用了expvar,于是你间接引用了超级多的包。
$ go list -f {{.Deps}} expvar
[bufio bytes compress/flate compress/gzip crypto crypto/aes crypto/cipher crypto/des crypto/dsa crypto/elliptic crypto/hmac crypto/md5 crypto/rand crypto/rc4 crypto/rsa crypto/sha1 crypto/subtle crypto/tls crypto/x509 crypto/x509/pkix encoding/asn1 encoding/base64 encoding/binary encoding/json encoding/pem errors fmt hash hash/crc32 io io/ioutil log math math/big math/rand mime mime/multipart net net/http net/textproto net/url os path path/filepath reflect runtime runtime/cgo runtime/debug sort strconv strings sync sync/atomic syscall time unicode unicode/utf16 unicode/utf8 unsafe]
看看fmt引用了啥:
$ go list -f {{.Deps}} fmt
[errors io math os reflect runtime strconv sync sync/atomic syscall time unicode/utf8 unsafe]

Liigo Zhuang

unread,
Mar 30, 2012, 3:24:09 AM3/30/12
to golang...@googlegroups.com
哦,明白了。想不到啊。

--
来自: Golang-China ~ 中文Go语言技术邮件列表
详情: http://groups.google.com/group/golang-china
官网: http://golang-china.org/
IRC: irc.freenode.net #golang-china
@golangchina

Andy W. Song

unread,
Mar 30, 2012, 3:37:41 AM3/30/12
to golang...@googlegroups.com
话说这个expvar到底是做嘛用的?

2012/3/30 Liigo Zhuang <com....@gmail.com>



--
---------------------------------------------------------------
有志者,事竟成,破釜沉舟,百二秦关终属楚
苦心人,天不负,卧薪尝胆,三千越甲可吞吴

fango

unread,
Mar 30, 2012, 4:13:11 AM3/30/12
to golang...@googlegroups.com
应该是Export Variables的缩写。举例是运行godoc,然后 http://localhost:6060/debug/vars

fango

fango

unread,
Mar 30, 2012, 4:18:36 AM3/30/12
to golang...@googlegroups.com
我是想说expose variables.

fango

unread,
Mar 30, 2012, 4:29:14 AM3/30/12
to golang...@googlegroups.com
补充:go list -f {{.Deps | len}} net/http 可以看到实际是这个http很大,使用了56个导入,而expvar只多了两个。

fango

Liigo Zhuang

unread,
Mar 30, 2012, 4:51:57 AM3/30/12
to golang...@googlegroups.com
顶楼的代码在我电脑上会输出以下内容,大致包含了内存使用和命令行信息:
其他还在摸索中……
----------------------------------------------------------------------------------------------------------
memstats={"Alloc":286552,"TotalAlloc":298352,"Sys":2621440, ... <<very long>> ... }

cmdline=["D:\\Users\\liigo\\AppData\\Local\\Temp\\go-build209051666\\command-line-arguments\\_obj\\a.out.exe"] 
---------------------------------------------------------------------------------------------------------- 
Reply all
Reply to author
Forward
0 new messages