One way you could check for a particular type of error might be using the Error method on an error type to get the message from the error and compare that value to the type of error you’re looking for. Imagine that in your program, you want to show a message other than there was an error: uh oh when the error value is uh oh. One approach to handling this case would be to check the value returned from the Error method, like so:

if err.Error() == "uh oh" {
	// Handle 'uh oh' error.
	fmt.Println("oh no!")
}
Now, save your code and use go run to run your program again:

go run main.go
Checking the string value of err.Error() to see if it’s the value uh oh, as in the code above, would work in this case. But the code would not work if the uh oh error string is slightly different elsewhere in the program. Checking errors this way can also lead to significant updates to code if the error’s message itself needs to be updated because every place the error is checked would need to be updated. Take the following code, for example:

func giveMeError() error {
	return fmt.Errorf("uh h")
}

err := giveMeError()
if err.Error() == "uh h" {
	// "uh h" error code
}
Now, let’s say you were using one of these error values in the typo example from before:

var errUhOh = fmt.Errorf("uh h")

func giveMeError() error {
	return errUhOh
}

err := giveMeError()
if err == errUhOh {
	// "uh oh" error code
}
This time the output will show the generic error output for the 1 value, but it uses the custom oh no! message when it sees the errUhOh error returned from validateValue for 2:

Outputvalidating 1... there was an error: that's odd
validating 2... oh no!
validating 3... valid!

Recommend

How to Add Extra Information to Errors in Go Returning and Handling Errors in Go

Como Instalar o Git no CentOS 7 Configurando o Git

Como Instalar o Git no CentOS 7 Instalar o Git Opção Dois — Instalar o Git a Partir do Código-fonte

Como Instalar o Git no CentOS 7 Instalar o Git Opção Um — Instalar o Git com Yum

Установка и использование Docker в Ubuntu 20.04 Шаг 8 — Загрузка образов Docker в репозиторий Docker

Установка и использование Docker в Ubuntu 20.04 Шаг 7 — Внесение изменений в контейнер для образа Docker

Установка и использование Docker в Ubuntu 20.04 Шаг 6 — Управление контейнерами Docker

Установка и использование Docker в Ubuntu 20.04 Шаг 5 — Запуск контейнеров Docker

Установка и использование Docker в Ubuntu 20.04 Шаг 4 — Работа с образами Docker

Установка и использование Docker в Ubuntu 20.04 Шаг 3 — Использование команды Docker

Установка и использование Docker в Ubuntu 20.04 Шаг 2 — Настройка команды Docker без sudo (необязательно)

Установка и использование Docker в Ubuntu 20.04 Шаг 1 — Установка Docker

Comment développer un site web Drupal 9 sur votre machine locale en utilisant Docker et DDEV Étape 6 — Gérer le conteneur DDEV

Comment développer un site web Drupal 9 sur votre machine locale en utilisant Docker et DDEV Étape 4 — Vérifier vos autorisations

Comment développer un site web Drupal 9 sur votre machine locale en utilisant Docker et DDEV Étape 3 — Configuration de votre projet Drupal 9 Option 2 — Utilisation de la ligne de commande

Comment développer un site web Drupal 9 sur votre machine locale en utilisant Docker et DDEV Étape 3 — Configuration de votre projet Drupal 9

Comment développer un site web Drupal 9 sur votre machine locale en utilisant Docker et DDEV Étape 2 — Déployer un nouveau site Drupal 9 en utilisant DDEV

Comment développer un site web Drupal 9 sur votre machine locale en utilisant Docker et DDEV Étape 1 — Installer DDEV Option 2 — Installer DDEV sur Linux

Comment développer un site web Drupal 9 sur votre machine locale en utilisant Docker et DDEV Étape 1 — Installer DDEV Option 1 — Installation de DDEV sur macOS