Go Language Learning---errors

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Error

errors are anything you can describe yourself with a string. The main idea is to return the string by the predefined built -in interface type ' Error ', and the method ' error ':

Type Error Interface {    error () string}

fmtThis method is called automatically when you output one of several different print functions for a package error .

Here is an example

    the basic data type of package Mainimport ("FMT") type myfloat float64  //go cannot directly increment functions, only first define a own data type, func (Data myfloat) Error () string{    return FMT. Sprintf ("Cannot sqrt negative number:%f", data)}func sqrt (f float64) (Float64, error) {    If f < 0 {        var data my float        data = myfloat (f)        return 0, data    }    return 0, Nil}func main () {    fmt. Println (SQRT (2))    FMT. Println (Sqrt (-2))}






reference: http://go-tour-zh.appspot.com/#54

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.