github.com/huandu/go@v0.0.0-20151114150818-04e615e41150/misc/cgo/test/issue4029.go (about)

     1  // Copyright 2012 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build !windows
     6  
     7  package cgotest
     8  
     9  /*
    10  #include <dlfcn.h>
    11  #cgo linux LDFLAGS: -ldl
    12  */
    13  import "C"
    14  
    15  import (
    16  	"fmt"
    17  	"testing"
    18  )
    19  
    20  //export IMPIsOpaque
    21  func IMPIsOpaque() {
    22  	fmt.Println("isOpaque")
    23  }
    24  
    25  //export IMPInitWithFrame
    26  func IMPInitWithFrame() {
    27  	fmt.Println("IInitWithFrame")
    28  }
    29  
    30  //export IMPDrawRect
    31  func IMPDrawRect() {
    32  	fmt.Println("drawRect:")
    33  }
    34  
    35  //export IMPWindowResize
    36  func IMPWindowResize() {
    37  	fmt.Println("windowDidResize:")
    38  }
    39  
    40  func test4029(t *testing.T) {
    41  	loadThySelf(t, "IMPWindowResize")
    42  	loadThySelf(t, "IMPDrawRect")
    43  	loadThySelf(t, "IMPInitWithFrame")
    44  	loadThySelf(t, "IMPIsOpaque")
    45  }
    46  
    47  func loadThySelf(t *testing.T, symbol string) {
    48  	this_process := C.dlopen(nil, C.RTLD_NOW)
    49  	if this_process == nil {
    50  		t.Error("dlopen:", C.GoString(C.dlerror()))
    51  		return
    52  	}
    53  	defer C.dlclose(this_process)
    54  
    55  	symbol_address := C.dlsym(this_process, C.CString(symbol))
    56  	if symbol_address == nil {
    57  		t.Error("dlsym:", C.GoString(C.dlerror()))
    58  		return
    59  	}
    60  	t.Log(symbol, symbol_address)
    61  }