GO lang异常处理

I am new to go lang. How to achieve subtype inheritance in GO and handle exceptions in it? I am trying something to this but somehow I am not able to get it to work.

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");
        }
    }
}

As people already has pointed out, Go is very different from Java.
That means you will not have something "very much similar" to the Java code.

Embedding instead of inheritance

Go does not have inheritance as you might be familiar with it. The closest you might find is called embedding.

And while an embedded method might be shadowed by a method of the parent and act as a sort of override, this is not a common way to solve your programming tasks in Go.

Errors instead of Exceptions

Panics are not to be used as exceptions. If you want to write Go code, you return errors to inform the calling function/method that something went wrong.

What your code might look like in 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())
    }
}

Output:

m1 in Struct1

Playground: http://play.golang.org/p/VrhvtQDXCx