Go calls the specified function based on a string

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

Follow up with the study, stay for reference

1, the Novice web design, do not want to use third-party library; so Handlefunc () wrote full screen is ... It would be nice to think of the idea of simulating MVC and automatically invoking the specified controller method according to the path of the URL.

2, try to use the Go Reflex function, the initial realization of the principle algorithm;

3, need to introduce "reflect" package, using reflect. ValueOf () method, where the parameter address must be passed in, otherwise it returns the reflect. The value type can only query the method of static declaration, and cannot get the method of dynamic binding;

4, even if the parameter address is passed in, it returns the reflect. The value type, also cannot get the method name; Go should mean that the value and type are processed separately, and that there is a name and other information in the type;

4. The type () method of the return value needs to be called again, so that the method name can be queried;

5, then the method name and method pointer into the map, followed by Map[name] in the way of the method name string to call the corresponding method;

The package Mainimport ("FMT" "reflect")//defines the Controller function map type to facilitate subsequent quick use of type Controllermapstype map[string]reflect. VALUE//Declaration Controller function Map type variable var controllermaps controllermapstype//define router struct type routers struct {}//for router structure additional function controller function, The value passes the func (this *routers) Login (msg string) {Fmt. Println ("Login:", msg)}//for the router structure additional function controller function, reference passing func (this *routers) changename (msg *string) {fmt. Println ("ChangeName:", *msg) *msg = *msg + "Changed"}func main () {var rutest routerscrmap: = Make (Controllermapstype, 0)// To create a reflection variable, note that the address of the rutest variable needs to be passed in;//The address can only reflect the routers statically defined method VF: = Reflect. ValueOf (&rutest) VFT: = VF. Type ()//number of Read methods Mnum: = VF. Nummethod () fmt. Println ("Nummethod:", mnum)//The method of traversing the router and storing it in the controller mapping variable for i: = 0; i < Mnum; i++ {mname: = vft. Method (i). Namefmt.println ("Index:", I, "MethodName:", mname) crmap[mname] = VF. Method (i)//<<<}//demo teststr: = "Hello Go"//Create a parameter list to be passed in with the calling method parms: = []reflect. Value{reflect. ValueOf (TESTSTR)}//uses the method name string to invoke the specified method crmap["Login". Call (parms)//Create a parameter list to pass in with the calling method parms = []reflect. Value{reflect. ValuEOf (&AMP;TESTSTR)}//calls the specified method crmap["ChangeName" using the method name string. Call (parms)//visible, the value of TESTSTR has been modified fmt.println ("TESTSTR:", Teststr)}


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.