# How to use glossaries' \defglsentryfmt to typeset the first occurrance of an entry?

I want to set the first occurrance of a glossary entry in italics. From this answer to a related question, I got the following MWE:

\documentclass{article}

\usepackage{glossaries}
\makeglossaries

\defglsdisplayfirst[\glsdefaulttype]{\textit{#1}}

\newglossaryentry{term}{name={technical term}, description=\nopostdesc}

\begin{document}
First occurrance of \gls{term}, second occurrance of \gls{term}.

\end{document}


However, it has two problems. First, \defglsdisplayfirst is apparently deprecated, and second, I get unwanted horizontal space when using it:

I have seen similar questions on how to solve this without using \defglsdisplayfirst, but they all concern acronyms. I use both acronyms and other entries which are not acronyms, and I want this to apply to all kinds of glossary entries.

As part of the deprecation warning, it says that I should use \defglsentryfmt instead, but I can't figure out how to use it.

After looking more closely into the manual and how \glsentryfmt is originally defined, I managed to figure it out. =)

This gives me what I want:

\documentclass{article}

\usepackage{glossaries}
\makeglossaries

\defglsentryfmt{%
\ifglsused{\glslabel}{%
\glsgenentryfmt%
}{%
% Typeset first use
\textit{\glsgenentryfmt}%
}%
}

\newglossaryentry{term}{name={technical term}, description=\nopostdesc}

\begin{document}
First occurrance of \gls{term}, second occurrance of \gls{term}.

\end{document}