func (check *Checker) typInternal(e0 syntax.Expr, def *Named) (T Type) {switch e := e0.(type) {case*syntax.BadExpr:// ignore - error reported beforecase*syntax.Name:var x operand check.ident(&x, e, def, true)switch x.mode {case typexpr: typ := x.typ def.setUnderlying(typ)return typcase invalid:// ignore - error reported beforecase novalue: check.errorf(&x, "%s used as type", &x)default: check.errorf(&x, "%s is not a type", &x) }case*syntax.SelectorExpr:case*syntax.IndexExpr:case*syntax.ParenExpr:case*syntax.ArrayType:case*syntax.SliceType:case*syntax.DotsType:case*syntax.StructType:case*syntax.Operation:case*syntax.FuncType:case*syntax.InterfaceType:case*syntax.MapType:case*syntax.ChanType: typ :=new(Chan) def.setUnderlying(typ) dir := SendRecvswitch e.Dir {case0:// nothing to docase syntax.SendOnly: dir = SendOnlycase syntax.RecvOnly: dir = RecvOnlydefault: check.errorf(e, invalidAST+"unknown channel direction %d", e.Dir)// ok to continue } typ.dir = dir typ.elem = check.varType(e.Elem)return typdefault: check.errorf(e0, "%s is not a type", e0) check.use(e0) } typ := Typ[Invalid] def.setUnderlying(typ)return typ }
通过方法签名可知:该方法通过一个表达式推导出一个类型。其中每个 case 都对应一个类型表达式,这里只留下syntax.Name与syntax.ChanType进行演示。