When your network hands out IPv6 but the route out is flaky, programs try the IPv6 address first and hang before they fall back. /etc/gai.conf changes that order for everything that uses the glibc resolver.
On Debian and Ubuntu the file already contains the line you need, commented out, below a remark about sites that prefer IPv4. Remove the # so it reads:
precedence ::ffff:0:0/96 100
IPv4 addresses are represented in that table as ::ffff:0:0/96. Raising their precedence to 100 puts them above IPv6 when getaddrinfo() sorts the addresses of a host. There is no service to restart; each program reads the file when it does a lookup, though long running ones may cache it.
Check the result with:
getent ahosts example.com | head -n 3
The first lines should now be IPv4 addresses.
Read the man page before you add more lines. As soon as the file contains one precedence line, the whole built in default table is replaced by what you wrote. To keep the other defaults, copy all the precedence lines from the example in the file and change only the IPv4 one. I also see this as a workaround: the real fix is the IPv6 route. Programs that do their own resolving, like many Go binaries, ignore this file.
Further reading: gai.conf(5) manual page, getaddrinfo(3) manual page