Go reflect TypeOf and valueof

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.


Learning Go's reflection is of great value in understanding the underlying implementation of go and the interface mechanism. This article from a source program to experience the go reflection of the two core functions typeof and valueof, especially the way to construct the type instance according to type.


Package Mainimport ("FMT" "reflect") type Speaker interface {Speak () Stringset (string)}type Teacher struct {Name string} Func (this *teacher) Speak () string {return this. Name}func (This *teacher) Set (name string) {this. Name = Name}func Testref (s Speaker) (t reflect. Type) {reflectval: = reflect. ValueOf (s)//t = reflect. Indirect (Reflectval). Type () T = Reflectval.elem (). Type () fmt. Printf ("reflect. ValueOf (%V). Elem (). Type () =%v\n ", s,t) Return}func TestRef2 (S Speaker) (t reflect. Type) {//t = reflect. TypeOf (s). Elem ()//fmt. Printf ("reflect. TypeOf (%V). Elem () =%v\n ", s,t) T = reflect. TypeOf (s) fmt. Printf ("reflect. TypeOf (%V) =%v\n ", s,t) Return}func TestRef3 (S *teacher) (t reflect. Type) {t = reflect. TypeOf (s). Elem () fmt. Printf ("reflect. TypeOf (%V). Elem () =%v\n ", S,t) Return}func main () {te: = &teacher{name:" NewYear ",}fmt. Printf ("source=%v\n", TE) fmt. Printf ("\ n") var tea Speaker = te T1: = Testref (TEA)//default is to create a 0 value instance, string is "" If m, OK: = reflect. New (T1). Interface (). (Speaker); OK {fmt. Printf ("reflect. New (%V).Interface (). (Speaker) =%v\n ", T1, M) fmt. Printf ("se. Speak () =%v\n ", M.speak ()) M.set (" the ") fmt. Printf ("reflect. New (%V). Interface (). (Speaker) =%v\n ", T1, M) fmt. Printf ("se. Speak () =%v\n ", M.speak ())}fmt. Printf ("Source Instance te=%v\n", TE) fmt. Printf ("\ n") t2: = TestRef2 (tea)//if m, OK: = reflect. New (T2). Interface (). (Speaker); OK {if m, OK: = reflect. New (T2. Elem ()). Interface (). (Speaker); OK {fmt. Printf ("reflect. New (%v.elem ()). Interface (). (Speaker) =%v\n ", T2, M) fmt. Printf ("se. Speak () =%v\n ", M.speak ()) M.set (" ") fmt. Printf ("reflect. New (%v.elem ()). Interface (). (Speaker) =%v\n ", T2, M) fmt. Printf ("se. Speak () =%v\n ", M.speak ())}fmt. Printf ("Source Instance te=%v\n", TE) fmt. Printf ("\ n") T3: = TESTREF3 (TE) if m, OK: = reflect. New (T3). Interface (). (Speaker); OK {fmt. Printf ("reflect. New (%V). Interface (). (Speaker) =%v\n ", T3, m) fmt. Printf ("se. Speak () =%v\n ", M.speak ()) M.set (" ") fmt. Printf ("reflect. New (%V). Interface (). (Speaker) =%v\n ", T3, m) fmt. Printf ("se. Speak () =%v\n ", M.speak ())}fmt. Printf ("Source Instance te=%v\n",TE) fmt. Printf ("\ n") fmt. Printf ("reflect. TypeOf (tea) =%v\n ", reflect. TypeOf (tea)) fmt. Printf ("reflect. TypeOf (tea). Elem () =%v\n ", reflect. TypeOf (tea). Elem ()) fmt. Printf ("reflect. ValueOf (tea) =%v\n ", reflect. ValueOf (tea)) fmt. Printf ("reflect. ValueOf (tea). Elem () =%v\n ", reflect. ValueOf (tea). Elem ()) fmt. Printf ("\ n") if SE, OK: = reflect. ValueOf (tea). Interface (). (Speaker); OK {//if SE, OK: = reflect. ValueOf (TE). Interface (). (Speaker); OK {fmt. Printf ("reflect. ValueOf (%V). Interface (). (Speaker) =%v\n ", tea, se) fmt. Printf ("se. Speak () =%v\n ", SE. Speak ()) se. Set ("") fmt. Printf ("reflect. ValueOf (%V). Interface (). (Speaker) =%v\n ", tea, se) fmt. Printf ("se. Speak () =%v\n ", SE. Speak ())}fmt. Printf ("Source Instance te=%v\n", TE) fmt. Printf ("\ n")//if se, OK: = reflect. New (reflect. TypeOf (TE). Elem ()). Interface (). (Speaker); OK {if SE, OK: = reflect. New (reflect. TypeOf (tea). Elem ()). Interface (). (Speaker); OK {fmt. Printf ("reflect. New (reflect. TypeOf (%V). Elem ()). Interface (). (Speaker) =%v\n ", Tea,se) fmt. Printf ("se. Speak () =%v\n ",Se. Speak ()) se. Set ("2018") Fmt. Printf ("reflect. New (reflect. TypeOf (%V). Elem ()). Interface (). (Speaker) =%v\n ", Tea,se) fmt. Printf ("se. Speak () =%v\n ", SE. Speak ())}fmt. Printf ("Source Instance te=%v\n", TE) fmt. Printf ("\ n") if SE, OK: = reflect. New (reflect. TypeOf (TE). Elem ()). Interface (). (Speaker); OK {fmt. Printf ("reflect. New (reflect. TypeOf (%V). Elem ()). Interface (). (Speaker) =%v\n ", Te,se) fmt. Printf ("se. Speak () =%v\n ", SE. Speak ()) se. Set ("2019") FMT. Printf ("se. Speak () =%v\n ", SE. Speak ()) fmt. Printf ("reflect. New (reflect. TypeOf (%V). Elem ()). Interface (). (Speaker) =%v\n ", Te,se)}fmt. Printf ("Source Instance te=%v\n", TE) fmt. Printf ("\ n")}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.