Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GO-lang异常处理_Go - Fatal编程技术网

GO-lang异常处理

GO-lang异常处理,go,Go,我是go lang的新手。如何在go中实现子类型继承并在其中处理异常?我正在尝试一些方法来解决这个问题,但不知何故,我无法让它发挥作用 import java.io.*; import java.rmi.*; class class1 { public void m1() throws RemoteException { System.out.println("m1 in class1"); } } class class2 extends class

我是go lang的新手。如何在go中实现子类型继承并在其中处理异常?我正在尝试一些方法来解决这个问题,但不知何故,我无法让它发挥作用

import java.io.*;
import java.rmi.*;

class class1
{
    public void m1() throws RemoteException
    {
        System.out.println("m1 in class1");
    }
}

class class2 extends class1
{
    public void m1() throws IOException
    {
        System.out.println("m1 in class2");
    }
}

class ExceptionTest2
{
    public static void main(String args[])
    {
        class1 obj = new class1();

        try{
            obj.m1();
        }
        catch(RemoteException e){
            System.out.println("ioexception");
        }
    }
}

正如人们已经指出的,Go与Java非常不同。
这意味着您将不会有与Java代码“非常相似”的东西

嵌入而不是继承

Go没有您可能熟悉的继承。您可能找到的最接近的是调用

虽然嵌入方法可能会被父方法隐藏,并充当某种覆盖,但这并不是解决Go中编程任务的常用方法

错误而非异常

恐慌不能作为例外。如果要编写Go代码,则返回错误以通知调用函数/方法出错

您的代码在围棋中的外观:

package main

import (
    "errors"
    "fmt"
)

var (
    RemoteError = errors.New("A remote error occured")
    IOError     = errors.New("An IO error occured")
)

type Struct1 struct{}

func (s *Struct1) m1() error {
    fmt.Println("m1 in Struct1")
    return nil // or RemoteError
}

type Struct2 struct {
    Struct1
}

func (s *Struct2) m1() error {
    fmt.Println("m1 in Struct2")
    return nil // or IOError 
}

func main() {
    s1 := &Struct1{}

    err := s1.m1()
    if err != nil {
        fmt.Println(err.Error())
    }
}
m1 in Struct1
输出:

package main

import (
    "errors"
    "fmt"
)

var (
    RemoteError = errors.New("A remote error occured")
    IOError     = errors.New("An IO error occured")
)

type Struct1 struct{}

func (s *Struct1) m1() error {
    fmt.Println("m1 in Struct1")
    return nil // or RemoteError
}

type Struct2 struct {
    Struct1
}

func (s *Struct2) m1() error {
    fmt.Println("m1 in Struct2")
    return nil // or IOError 
}

func main() {
    s1 := &Struct1{}

    err := s1.m1()
    if err != nil {
        fmt.Println(err.Error())
    }
}
m1 in Struct1

游乐场:

正如人们已经指出的那样,围棋与Java非常不同。
这意味着您将不会有与Java代码“非常相似”的东西

嵌入而不是继承

Go没有您可能熟悉的继承。您可能找到的最接近的是调用

虽然嵌入方法可能会被父方法隐藏,并充当某种覆盖,但这并不是解决Go中编程任务的常用方法

错误而非异常

恐慌不能作为例外。如果要编写Go代码,则返回错误以通知调用函数/方法出错

您的代码在围棋中的外观:

package main

import (
    "errors"
    "fmt"
)

var (
    RemoteError = errors.New("A remote error occured")
    IOError     = errors.New("An IO error occured")
)

type Struct1 struct{}

func (s *Struct1) m1() error {
    fmt.Println("m1 in Struct1")
    return nil // or RemoteError
}

type Struct2 struct {
    Struct1
}

func (s *Struct2) m1() error {
    fmt.Println("m1 in Struct2")
    return nil // or IOError 
}

func main() {
    s1 := &Struct1{}

    err := s1.m1()
    if err != nil {
        fmt.Println(err.Error())
    }
}
m1 in Struct1
输出:

package main

import (
    "errors"
    "fmt"
)

var (
    RemoteError = errors.New("A remote error occured")
    IOError     = errors.New("An IO error occured")
)

type Struct1 struct{}

func (s *Struct1) m1() error {
    fmt.Println("m1 in Struct1")
    return nil // or RemoteError
}

type Struct2 struct {
    Struct1
}

func (s *Struct2) m1() error {
    fmt.Println("m1 in Struct2")
    return nil // or IOError 
}

func main() {
    s1 := &Struct1{}

    err := s1.m1()
    if err != nil {
        fmt.Println(err.Error())
    }
}
m1 in Struct1

操场:

@user2864740感谢您的评论。更新了问题,我的实际问题是这个。@user2864740问题更新了。请告诉我是否可以改进我的问题。那么,异常处理或golang如何处理子类型关系?Go不是Java,它有不同的语言特性。也许你可以问一个描述你想要达到什么效果的问题,你会得到一个关于如何在Go中做到这一点的有用答案。由于函数可以返回多个值,惯用的方法是将错误变量与结果一起返回。有一个panic()和recover(),类似于异常,但它们的使用更为有限,通常适用于不可恢复或异常错误。请查看错误管理。@user2864740谢谢您的评论。更新了问题,我的实际问题是这个。@user2864740问题已更新。请告诉我是否可以改进我的问题。那么,异常处理或golang如何处理子类型关系?Go不是Java,它有不同的语言特性。也许你可以问一个描述你想要达到什么效果的问题,你会得到一个关于如何在Go中做到这一点的有用答案。由于函数可以返回多个值,惯用的方法是将错误变量与结果一起返回。有一个panic()和recover(),类似于异常,但它们的使用更为有限,通常适用于不可恢复或异常错误。请看一看错误管理。