我调用一个返回对的函数(在示例os.Open中),我想将返回的对直接作为参数传递给另一个函数(不使用中间变量)。我尝试了以下两种方法,但均未成功。 其他语言(例如F#)允许在函数调用上执行模式匹配。如何在Go中实现相同的目标?
func checkError(f *os.File, e error, m string) interface{} {
if e != nil {
/*Print m and panic*/
}
return f
}
func f1(path string) {
checkError(os.Open(path), "Can't Open File") //ERROR
}
func checkError((f *os.File, e error), m string) interface{} { //ERROR
if e != nil { /*Print m and panic*/}
return f
}
func f1(path string) {
checkError(os.Open(path), "Can't Open File")
}
答案 0 :(得分:2)
允许这样做会导致模棱两可的行为;但是您可以关闭最后一个参数并返回部分应用的函数:
func checkError(m string) func(*os.File, error) {
return func(f *os.File, e error) {
if e != nil {
// do stuff with m
}
}
checkError("Can't Open File")(os.Open(path))
或者,反过来:
func checkError(f *os.File, e error) func(string) {
return func(m string) {
if e != nil {
// do stuff with m
}
}
checkError(os.Open(path))("Can't Open File")
答案 1 :(得分:0)
我发现它有点不一致,因为下面的方法可以正常工作,但是当添加额外的位置参数时,编译器不喜欢它。
func checkError(f *os.File, e error) interface{} {
if e != nil {
/*Print m and panic*/
}
return f
}
func f1(path string) {
checkError(os.Open(path)) //ERROR
}
很明显,您可以直接推送返回值,不确定丢失多少,因为如果错误不为零,您将感到恐慌
func checkError(f *os.File, e error, m string) interface{} {
if e != nil {
/*Print m and panic*/
}
return f
}
func f1(path string) {
file, e := os.Open(path)
checkError(file, e, "Can't Open File")
}
另一种想法是,您可以传递函数并在checkError
中进行工作
type OsFunction func(string)(* os.File, error)
func checkError(osFunction OsFunction, path string, m string) interface{} {
f, e := osFunction(path)
if e != nil {
/*Print m and panic*/
}
return f
}
func f2(path string) {
checkError2(os.Open, path, "Can't Open File")
}
答案 2 :(得分:-1)
感谢Jack的提示,我记得函数也可以返回。 以下解决方案对我有用
//pick the gallery images
ClipData mClipData = data.getClipData();
ArrayList < Uri > mArrayUri = new ArrayList < Uri > (); //list of images
for (int i = 0; i < mClipData.getItemCount(); i++) { //mClipData displayed picked images count
ClipData.Item item = mClipData.getItemAt(i);
Uri uri = item.getUri();
mArrayUri.add(uri); /// add the images to list
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mArrayUri.get(i)); //pass the images with position
System.out.println("sizebitmap::" + bitmap);
saveImage(bitmap); //it calls the server upload method
// Get the cursor
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imageEncoded = cursor.getString(columnIndex);
imagesEncodedList.add(imageEncoded);
cursor.close();
galleryAdapter = new GalleryAdapter(getApplicationContext(), mArrayUri);
gvGallery.setAdapter(galleryAdapter);
gvGallery.setVerticalSpacing(gvGallery.getHorizontalSpacing());
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) gvGallery
.getLayoutParams();
mlp.setMargins(0, gvGallery.getHorizontalSpacing(), 0, 0);
}
//server upload code
try {
HttpClient client = new HttpClient(url);
client.connectForMultipart();
client.addFormPart("name", "ren");
client.addFormPart("machine_element_name_id", "9");
client.addFilePart("file", "ic_launcher_background.png", bytes.toByteArray());
client.finishMultipart();
String data = client.getResponse();
System.out.println("res:" + data);
} catch (Throwable t) {
t.printStackTrace();
}