The structure and electronic structure properties of GaN doped with Mg are studied by plane-wave pseudopotential density functional theory method. By calculating the binding energy of systems with Mg replacing the Ga and N atoms respectively, it is easier for Mg atoms to replace the Ga atoms, which is consistent with the results of others. The lattice constants a and c are slightly increased after doping, and the case under high pressure is similar. With the pressure increasing from 0 GPa to 20 GPa, the band gaps of the GaN and the doped-GaN increase by about 39.1% and 38.4%, respectively.