This letter investigates the optimal antenna spacing for a near-field XL-MIMO communication system from the perspective of the array gain. Specifically, using the Green's function-based channel model, the letter analyzes the channel capacity, which is related to the effective degrees-of-freedom (EDoF). Then, the letter further investigates the applicability of two EDoF estimation methods. To increase EDoF, this letter focuses on analyzing the impact of antenna spacing. Furthermore, from the perspective of the array gain, the letter derives an approximate closed-form expression of the optimal antenna spacing, at which EDoF is maximized and the array gain at the antenna nearest to the focused antenna of the transmit array becomes zero. Finally, numerical results verify the main results of this letter.