border@ubuntu:~/work/go/tutorial$ cat helloworld.go
package main
import fmt “fmt”
func 输出函数(s string) {
fmt.Printf(s)
}
func main() {
fmt.Printf(”Hello, 中文\n”)
变量 := “传说中的中文编程\n”
输出函数(变量)
}
border@ubuntu:~/work/go/tutorial$ 8g helloworld.go
border@ubuntu:~/work/go/tutorial$ 8l -o helloworld helloworld.8
border@ubuntu:~/work/go/tutorial$ ./helloworld
Hello, 中文
传说中的中文编程
–
Bian Jiang
Blog: http://www.wifihack.net/
在Go语言中,每个type都是不同的,这里的type和c语言中的typedef还是有些区别的,
在Go中不能用type派生出来的int类型数据去替换内置的int类型. 比如:
———————————————————-
border@wifihack:~/work/go/tutorial$ cat type1.go
border@wifihack:~/work/go/tutorial$ 8g type1.go
type1.go:13: cannot use a (type MyTypeA) as type int in function argument
你不能把type派生出来的int类型的MyTypeA传递给printInt(i int)函数。所以 MyTypeA 与 int 不能直接替换。
同样的道理如果你自己定义了2个相同的类型比如:
a = b 或 b = a 都是错误的,编译不通过。
如果你非要使用printInt(i int)这个函数也行,但是必须把传进去的数据类型转换为int类型。
就可以解决编译问题,但是Go提供了另一种解决方法接口(interface),你可以通过一个空接口来实现, 例如:
———————————————————-
border@wifihack:~/work/go/tutorial$ cat type2.go
package main
import “fmt”
type MyTypeA int
type Empty interface {}
func printInt(i Empty) {
fmt.Printf(”%d\n”, i)
}
func main() {
var a MyTypeA = 0
printInt(a)
}
border@wifihack:~/work/go/tutorial$ 8g type2.go
border@wifihack:~/work/go/tutorial$ 8l -o type2 type2.8
border@wifihack:~/work/go/tutorial$ ./type2
0
———————————————————-
如果使用接口(interface)就和类型无关,你也可以在printInt函数里面输出字符串看看效果:
———————————————————-
package main
import “fmt”
type MyTypeA int
type MyTypeB int
func printInt(i interface{}) {
fmt.Printf(”%d\n”, i)
fmt.Printf(”%s\n”, i)
}
func main() {
var a MyTypeA = 0
var b MyTypeB = 2
printInt(a)
printInt(b)
var s string = “Hello, 中文”
printInt(s)
}
border@wifihack:~/work/go/tutorial$ ./type
0
%s(main.MyTypeA=0)
2
%s(main.MyTypeB=2)
%d(string=Hello, 中文)
Hello, 中文
———————————————————-
参考: https://groups.google.com/group/golang-nuts/browse_thread/thread/bf4174fc5cbafa13#870a9034b11a8651
GCC项目组织宣布,
允许gccgo(Google 推出的新语言GO)增加到gcc的主分支,更详细的信息可能要在GCC4.5或之后的版本中体现。 也就是说在不远的将来GO语言将是Linux的标配。
GO语言: http://www.golang.org
GO中文资料参考: http://golang-china.org
GO中文用户组: https://groups.google.com/group/golang-china/
git config --global user.name "Bian Jiang"
git config --global user.email borderj@gmail.com
git svn clone https://golang-china.googlecode.com/svn/trunk -s
git remote add origin git@github.com:border/golang-china.git
git push origin master
git svn rebase // 本地与Goolge Code 代码同步
git push // 上传本地的代码到github
git svn dcommit // 上传本地的代码到Google Code
在Github上创建ssh授权参考: http://wifihack.net/blog/2008/12/permission-denied-on-github/
–EOF–
http://www.wifihack.net
Recent Comments