Dimas Maulana

Dimas Maulana

Developer

Welcome to my website! I am a developer with a current focus on React and Go. My experience encompasses both front-end and back-end development, enabling me to design and develop seamless and efficient applications.

Golang Go Get Postgres Error

You are dealing with error handling in Go when working with PostgreSQL using the pq package. The code you provided demonstrates two different ways to handle and extract error information from a pq.Error type.

Let’s break down both of these code snippets:

  1. Using Type Assertion:
1
2
pqErr := err.(*pq.Error)
log.Println(pqErr.Code)

In this code, you are using a type assertion to check if the err is of type *pq.Error, and if it is, you extract the Code field from the pq.Error struct and log it. This approach assumes that err is a pq.Error type, and if it’s not, it will result in a runtime panic. So, it’s essential to be sure that err is indeed of type *pq.Error before using this approach.

Bash Shell Ignore Error on Particular Command

In Bash scripting, you can use the || true construct to ignore errors for a particular command or script. This is a common technique used to ensure that a script continues executing even if a specific command fails. Here’s how it works:

1
particular_script || true

In this example, particular_script is the command or script that you want to run, and || true is added at the end of the command. The || operator is used for conditional execution. It means that if particular_script fails (returns a non-zero exit status), the true command will always execute, effectively ignoring the error and allowing the script to continue running.

Delve Dlv Not Restart When Update Files on Golang Cosmtrek Air 12710…

If you’re facing issues with Delve (dlv) not restarting when updating files in your Go project using Cosmtrek/Air version 1.27.10 and encountering a “port address already in use” error, there are a couple of solutions you can try. This problem often occurs due to lingering Delve processes that prevent the tool from restarting properly.

Solution 1: Revert to a Previous Version

You can revert to a previous version of Cosmtrek/Air where this issue might not exist. To do this, follow these steps:

Bash Script to Get the Directory of the Script File

In Bash scripting, it’s often useful to determine the directory where the script file is located. This can be particularly important if your script needs to access other files or resources relative to its own location. Here’s a Bash script snippet that accomplishes this task:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/bin/bash

# Get the directory of the script file
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)

# Check if SCRIPT_DIR is empty or not
if [ -z "$SCRIPT_DIR" ]; then
  echo "Failed to determine the script directory."
  exit 1
fi

# Now, you can use SCRIPT_DIR for your operations
echo "The script is located in the directory: $SCRIPT_DIR"

Here’s a breakdown of what this script does:

Shell Bash Check if Environment Exist

Your provided code snippet appears to be a Bash script that checks if the TARGET_PATH environment variable is empty and, if so, sets it to ~/go by appending an export statement to the .bashrc file. This is a common technique to ensure that environment variables are set with default values if they are not already defined.

Here’s a breakdown of what the code does:

  1. if [[ -z "${TARGET_PATH}" ]]; then: This line checks if the TARGET_PATH environment variable is empty (i.e., its value is not set). The -z flag is used to test if a string is empty.

Shell Bash Check PATH Environment Exist

It looks like you are trying to check if a specific directory is included in the PATH environment variable in a Bash script. The code you provided is almost correct, but it has a small issue. You can modify it as follows to make it work correctly:

1
2
3
4
5
6
7
CHECK_PATH="/root/go/bin"

if [[ ":$PATH:" == *":$CHECK_PATH:"* ]]; then
    echo "Path found in PATH environment. Skipping configuration..."
else
    echo "Path not found in PATH environment. You may need to add it."
fi

Here’s a breakdown of the changes made:

0%