代码是这样的:packagemainimport("fmt""io""net/http""log")//获取大小的借口typeSizerinterface{Size()int64}//helloworld,thewebserverfuncHelloServer(whttp.ResponseWriter,r*http.Request){if"POST"==r.Method{file,_,err:=r.FormFile("userfile")iferr!=nil{http.Error(w,err.Error(),500)return}fmt.Fprintf(w,"上传文件的大小为:%d",file.(Sizer).Size())return}//上传页面w.Header().Add("Content-Type","text/html")w.WriteHeader(200)html:=` Sendthisfile:`io.WriteString(w,html)}funcmain(){http.HandleFunc("/hello",HelloServer)err:=http.ListenAndServe(":12345",nil)iferr!=nil{log.Fatal("ListenAndServe:",err)}}现在问题在于这一句:file.(Sizer).Size()。Sizer的interface明显是程序里定义的,没有任何实现接口的代码,但是这个语句是怎么工作的?
慕桂英546537
相关分类