GoLang
xxxxxxxxxx
package main
import (
"errors"
"fmt"
)
func f1(arg int) (int, error) {
if arg == 42 {
return -1, errors.New("can't work with 42")
}
return arg + 3, nil
}
type argError struct {
arg int
prob string
}
func (e *argError) Error() string {
return fmt.Sprintf("%d - %s", e.arg, e.prob)
}
func f2(arg int) (int, error) {
if arg == 42 {
return -1, &argError{arg, "can't work with it"}
}
return arg + 3, nil
}
func main() {
for _, i := range []int{7, 42} {
if r, e := f1(i); e != nil {
fmt.Println("f1 failed:", e)
} else {
fmt.Println("f1 worked:", r)
}
}
for _, i := range []int{7, 42} {
if r, e := f2(i); e != nil {
fmt.Println("f2 failed:", e)
} else {
fmt.Println("f2 worked:", r)
}
}
_, e := f2(42)
if ae, ok := e.(*argError); ok {
fmt.Println(ae.arg)
fmt.Println(ae.prob)
}
}
f1 worked: 10 f1 failed: can't work with 42 f2 worked: 10 f2 failed: 42 - can't work with it 42 can't work with it
JavaScript - Verifique se uma string começa e termina com determinados caracteres
Python - Verifique se um número é positivo, negativo ou 0
Linux - Como instalar o anc-api-tools
Java - Inverter um número
Linux - Como instalar o curvedns
C - Contar o número de dígitos em um inteiro
C - Encontre o maior número usando a alocação de memória dinâmica
Python - Bate-papo de E/S Bate-papo muito fácil
JavaScript - Contar o número de vogais em uma string
C++ - Converter número binário para decimal e vice-versa
Kotlin - Calcular a potência de um número
Kotlin - Encontrar fatorial de um número
Python - Como ordenar um dicionário por valor?
C++ - Verificar se um número é primo ou não
Linux - Como instalar o zlib1g-dev
Kotlin - Calcular a soma dos números naturais
JavaScript - Encontre a soma dos números naturais
GoLang - Como você escreve strings de várias linhas
We have been online since 2021 and 1 millions of people around the globe have visited our website since then
More visitors every month