slice
slice := []int {lenth, 'b', 'c', 'd'}arr := [5]int{1,2,3}non-constant array bound length
总结:slice 和数据的区别在于 有没有定义长度
map
maps1 := make(map[string]string)
key1 := "key1"
maps1[key1] = key1
s,ok := maps1[key1]
if ok {
fmt.Println("hava key [",s,"]")
}
实现接口区别
golang
package main
type Dock struct {
Sound string
}
func (this Dock) voice(a VoiceIFS) VoiceIFS {
return this
}
package main
import "fmt"
type VoiceIFS interface {
voice(VoiceIFS) VoiceIFS
}
func main() {
dock:= Dock{Sound : " ga ga ga "}
var t VoiceIFS
t = dock
ifs := t.voice(dock)
fmt.Println(ifs)
}
结果:
{ ga ga ga }
java
public interface VoiceIFS {
VoiceIFS voice(VoiceIFS s) ;
}
public class Dock implements VoiceIFS {
private String sound ;
public String getSound() {
return sound;
}
public void setSound(String sound) {
this.sound = sound;
}
@Override
public VoiceIFS voice(VoiceIFS s) {
return this;
}
@Override
public String toString() {
return "Dock{" +
"sound='" + sound + '\'' +
'}';
}
}
public class TestVoiceIFS {
public static void main(String[] args) {
VoiceIFS s = new Dock();
((Dock) s).setSound(" ga ga ga");
VoiceIFS voice = s.voice(s);
System.out.println(voice);
}
}
结果:
Dock{sound=' ga ga ga'}
总结:
java 实现结果需要implement 关键词来指定实现具体接口,而golang的话只需要与某个接口中的某个方法名字相同即可,golang更加松散,但是给我的感觉来说还是java的设计相对来说好一点 结构比较清晰,且golang的接口名字估计不能相同,
### 通过switch type 判断 接口类型
func main() {
doSomething(111,float32(1),"111")
}
func doSomething(item... interface{}) {
for _,e := range item {
v := reflect.ValueOf(e)
typ := v.Interface()
switch e.(type) {
case int:
i := typ.(int)
i++
fmt.Println("this is int interface,then do something...",i)
case float32:
f := typ.(float32)
f += 1.08
fmt.Println("this is float32 or float64 interface,then do something...",f)
}
}
}