执行不接受参数的方法

时间:2018-07-16 15:43:15

标签: go reflection

给定一个结构的任意实例,我希望能够执行其所有不接受参数的公共方法。

例如,在下面的代码中,我希望能够在不知道它们存在的情况下执行X {}。Foo()和X {}。Bar()。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<article class="cards-item">
  <figure class="card product">
    <div class="card-image card-image-2lc"></div>
    <figcaption class="card-content">
      <h1 class="card-title">McElroy</h1>
      <h2 class="card-title product__title">2LC Fusion Machine 1/2" CTS - 2" IPS Pipe</h2>
      <p class="card-text">The 2LC employs a semi-automatic locking cam (LC) system to maintain force during the cooling cycle. It incorporates McElroy's patented Centerline Guidance System and is designed to butt fuse tees, ells and other fittings.</p>
      <form action="" method="">
        <fieldset class="product">
          <div class="formrow">
            <div class="formitem col1">
              <label class="label req" for="pu-2lc">Pick Up Date</label>
              <input type="date" name="pu-2lc" id="pu-2lc/>
                </div>
              </div>
              <div class=" formrow ">
                <div class="formitem col1 ">
                  <label class="label req " for="rd-2lc ">Return Date</label>
                  <input type="date " name="rd-2lc " id="rd-2lc "/>
                </div>
              </div>
              <div class="formrow ">
                <div class="formitem col1of2 ">
                  <label class="label " for="country ">Country</label>
                  <select name="country " id="country " x-autocompletetype="country-name ">
                    <option selected="selected ">please choose</option>
                    <option value="bkf ">Bakersfield</option>
                    <option value="ch ">Chico</option>
                    <option value="fsn ">Fresno</option>
                    <option value="hyw ">Hayward</option>
                    <option value="mtc ">Manteca</option>
                    <option value="oak ">Oakley</option>
                    <option value="rwc ">Redwood City</option>
                    <option value="sac ">Sacramento</option>
                    <option value="sal ">Salinas</option>
                    <option value="sj ">San Jose</option>
                    <option value="sjf ">San Jose Fusion</option>
                    <option value="sr ">Santa Rosa</option>
                  </select>
                </div>
              </div>
            </fieldset>
            <div class="buttons ">
              <div class="back ">
                 <button class="primary button js-add-product " title="Add to cart " >Add to Cart</button>
              </div>
            </div>
          </form>
        </figcaption>
      </figure>
    </article>
      <aside class="cart js-cart ">
  <div class="cart__header ">
    <h1 class="cart__title ">Shopping cart</h1>
    <p class="cart__text ">
      <a class="button button--light js-toggle-cart " href="# " title="Close cart ">
        Close cart
      </a>
    </p>
  </div>
  <div class="cart__products js-cart-products ">
    <p class="cart__empty js-cart-empty ">
      Add a product to your cart
    </p>
    <div class="cart__product js-cart-product-template ">
      <article class="js-cart-product ">
        <h1>Product title</h1>
        <p>Pick up Date</p>
        <p>Return Date</p>
        <p>Location</p>
        <p>
          <a class="js-remove-product " href="# " title="Delete product ">
            Delete product
          </a>
        </p>
      </article>
    </div>
  </div>
  <div class="cart__footer ">
    <p class="cart__text ">
      <a class="button " href="# " title="Buy products ">
        Buy products
      </a>
    </p>
  </div>
</aside>

1 个答案:

答案 0 :(得分:5)

  1. reflect.TypeOf更改为reflect.ValueOf
  2. 检查type中的Method,以确保它接受零个参数
  3. Call调用方法
foo := reflect.ValueOf(X{})
for i := 0; i < foo.NumMethod(); i++ {
    method := foo.Method(i)
    if method.Type().NumIn() == 0 {
        method.Call(nil)
    }
}

https://play.golang.org/p/KYAfaFlEjVt