Let $f:X \rightarrow Y$ be a function. Prove that there exists a function $g:Y \rightarrow X$ such that $f \circ g = I_Y$ if and only if $f$ is a surjection.
I need help on proving the following:
$f$ is a surjection $\Rightarrow \exists g:Y \rightarrow X, f \circ g = I_y$
What I've tried:
$f$ is a surjection $\Rightarrow \forall y \in Y, \exists x \in X, f(x)=y$, definition of a surjection
From here, I thought of constructing a function, $g:Y \rightarrow X$, since all $y$s have some preimage, $x \in X$ . But this will not be possible if $f$ is not injective. In that case, the codomain of $g$ will need to be appropriately restricted, so that $y_1=y_2 \Rightarrow g(y_1)=g(y_2)$. But I can't restrict the codomain of $g$ since the conclusion requires the codomain of $g$ to be exactly $X$. I don't know what else to do. Also, is my attempt anywhere near a solution or am I thinking about this problem in a wrong way?