1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20/*! 21 * \brief Sample golang application to demonstrate function conversion to packed function. 22 * \file pack_func_convert.go 23 */ 24 25package main 26 27import ( 28 "fmt" 29 "./gotvm" 30) 31 32// sampleCb is a simple golang callback function like C = A + B. 33func sampleCb(args ...*gotvm.Value) (retVal interface{}, err error) { 34 for _, v := range args { 35 fmt.Printf("ARGS:%T : %v\n", v.AsInt64(), v.AsInt64()) 36 } 37 val1 := args[0].AsInt64() 38 val2 := args[1].AsInt64() 39 retVal = int64(val1+val2) 40 return 41} 42 43// main 44func main() { 45 // Welcome 46 47 // Simple convert to a packed function 48 fhandle, err := gotvm.ConvertFunction(sampleCb) 49 if err != nil { 50 fmt.Print(err) 51 return 52 } 53 fmt.Printf("Converted function\n") 54 55 retVal, err := fhandle.Invoke(10, 20) 56 fmt.Printf("Invoke Completed\n") 57 if err != nil { 58 fmt.Print(err) 59 return 60 } 61 fmt.Printf("Result:%v\n", retVal.AsInt64()) 62} 63