type Param map [string ]interface {}
type Show struct {
Param
}
func main1 () {
s := new (Show )
s .Param ["RMB" ] = 10000
}
type student struct {
Name string
}
func zhoujielun (v interface {}) {
switch msg := v .(type ) {
case * student , student :
msg .Name
}
}
type People struct {
name string `json:"name"`
}
func main () {
js := `{
"name":"11"
}`
var p People
err := json .Unmarshal ([]byte (js ), & p )
if err != nil {
fmt .Println ("err: " , err )
return
}
fmt .Println ("people: " , p )
}
type People struct {
Name string
}
func (p * People ) String () string {
return fmt .Sprintf ("print: %v" , p )
}
func main () {
p := & People {}
p .String ()
}
5.下面代码有没有问题,有的话问题在哪里?(20分)
func main () {
ch := make (chan int , 1000 )
go func () {
for i := 0 ; i < 10 ; i ++ {
ch <- i
}
}()
go func () {
for {
a , ok := <- ch
if ! ok {
fmt .Println ("close" )
return
}
fmt .Println ("a: " , a )
}
}()
close (ch )
fmt .Println ("ok" )
time .Sleep (time .Second * 100 )
}
var value int32
func SetValue (delta int32 ) {
for {
v := value
if atomic .CompareAndSwapInt32 (& value , v (v + delta )) {
break
}
}
}
type Project struct {}
func (p * Project ) deferError () {
if err := recover (); err != nil {
fmt .Println ("recover: " , err )
}
}
func (p * Project ) exec (msgchan chan interface {}) {
for msg := range msgchan {
m := msg .(int )
fmt .Println ("msg: " , m )
}
}
func (p * Project ) run (msgchan chan interface {}) {
for {
defer p .deferError ()
go p .exec (msgchan )
time .Sleep (time .Second * 2 )
}
}
func (p * Project ) Main () {
a := make (chan interface {}, 100 )
go p .run (a )
go func () {
for {
a <- "1"
time .Sleep (time .Second )
}
}()
time .Sleep (time .Second * 100000000000000 )
}
func main () {
p := new (Project )
p .Main ()
}
8.下面的代码有没有错误,有的话错在哪里?(20分)
func Foo (x interface {}) {
if x == nil {
fmt .Println ("empty interface" )
return
}
fmt .Println ("non-empty interface" )
}
func main () {
var x * int = nil
Foo (x )
}
9.请说出下面代码,执行时为什么会报错?(20分)
type Student struct {
name string
}
func main () {
m := map [string ]Student {"people" : {"zhoujielun" }}
m ["people" ].name = "wuyanzu"
}
type query func (string ) string
func exec (name string , vs ... query ) string {
ch := make (chan string )
fn := func (i int ) {
ch <- vs [i ](name )
}
for i , _ := range vs {
go fn (i )
}
return <- ch
}
func main () {
ret := exec ("111" , func (n string ) string {
return n + "func1"
}, func (n string ) string {
return n + "func2"
}, func (n string ) string {
return n + "func3"
}, func (n string ) string {
return n + "func4"
})
fmt .Println (ret )
}